In my app I send user information to server with http as follows, I get org.apache.http.client.HttpResponseException: Not Found and I have spend more time but could not solution.please help me find out the solution.
I get error at this line:
jsonResponse = httpClient.execute(httpPost, resHandler);
public String addNewCardDetail(String loggedin_id, String fn, String ln,
String cardholdername, String cardno,String cvv, String expdate, String add,
String state, String city, String pincode) {
jsonResponse = "";
httpClient = new DefaultHttpClient();
resHandler = new BasicResponseHandler();
httpPost = new HttpPost("http://xxx.xxx.xxx.xx/users/createCustomerCardAddress");
nameValuePairs = new ArrayList<NameValuePair>(11);
nameValuePairs.add(new BasicNameValuePair("loggedin_id",loggedin_id));
nameValuePairs.add(new BasicNameValuePair("firstname",fn));
nameValuePairs.add(new BasicNameValuePair("lastname",ln));
nameValuePairs.add(new BasicNameValuePair("cardHolderName",cardholdername));
nameValuePairs.add(new BasicNameValuePair("cardNumber",cardno));
nameValuePairs.add(new BasicNameValuePair("cardCvv",cvv));
nameValuePairs.add(new BasicNameValuePair("expDate",expdate));
nameValuePairs.add(new BasicNameValuePair("address",add));
nameValuePairs.add(new BasicNameValuePair("state",state));
nameValuePairs.add(new BasicNameValuePair("city",city));
nameValuePairs.add(new BasicNameValuePair("pincode",pincode));
Log.e("----------", "---------");
for (int i = 0; i < nameValuePairs.size(); i++) {
Log.e("add / edit new card", nameValuePairs.get(i).getName());
Log.e("add / edit new card", nameValuePairs.get(i).getValue());
}
Log.e("----------", "---------");
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
jsonResponse = httpClient.execute(httpPost, resHandler); // I get org.apache.http.client.HttpResponseException: Not Found
Log.e("add edit card response", "-"+jsonResponse);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonResponse;
}
My Log trace:
org.apache.http.client.HttpResponseException: Not Found
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71)
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:773)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:743)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
at com.fssd.spot.parser.ParsorClass.addNewCardDetail(ParsorClass.java:1098)
at com.fssd.spot.payment.BillingAddress$AddNewcard.doInBackground(BillingAddress.java:146)
at com.fssd.spot.payment.BillingAddress$AddNewcard.doInBackground(BillingAddress.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
The Api given is invalid so that I get this exception. now I get valid api and working. Thanks.
Please check server side coding... May be you are not getting proper response from server.
This error cause of invalid authorization for the URL. Please re-check the HTTP request and do it again.
Related
I am trying to insert UTF-8 characters(Like username in hindi or tamil language) in MySql with Codeigniter framework.
Here is my android side code:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(uploadApi);
try {
List<NameValuePair> nameValuePairs=new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("key1","विकिपीडिया"));
nameValuePairs.add(new BasicNameValuePair("key2","इण्टरनेट"));
UrlEncodedFormEntity form = new UrlEncodedFormEntity(nameValuePairs);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
and this is server side code in codeigniter:
public function create()
{
$name=$this->input->get_post("name") ;$text=$this->input->get_post("text");
$type=$this->input->get_post("type");$contact=$this->input->get_post("contact");
$mp=$this->input->get_post("mp");$mla=$this->input->get_post("mla");
$position=$this->input->get_post("position");
$config['upload_path'] = './uploads/';
$config['allowed_types'] = '*';
$config['max_size'] = '20000';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('image'))
{
$image="noimage.jpg";
echo $this->upload->display_errors();
}
else
{
$data = $this->upload->data();
$image=$data['file_name'];
}
$data['json']=$this->uploadmodel->insert($name,$image,$text,$contact,$mp,$mla,$type,$position);
echo "Thanks for Sharing";
}
All configuration is set to utf-8 in configuration file.
error is i am getting all string like ?????????
please tell me where i am doing wrong.
Thanks
I added this line:
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
Although i tried with it also before.
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
but the above runs successfully.
I have a login screen that when user clicks on login button, i need to add some information to URL address. This is my code for past data:
private String postData() {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(PropertyManager.getLoginURL());
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("u", Uri.encode(PropertyManager.getUserId())));
nameValuePairs.add(new BasicNameValuePair("p", Encryption.encrypt(PropertyManager.getPassword())));
nameValuePairs.add(new BasicNameValuePair("v", PropertyManager.VER));
nameValuePairs.add(new BasicNameValuePair("t", "0"));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httppost);
String responseBody = EntityUtils.toString(response.getEntity());
Log.i("Server Response 1: ", responseBody);
if (response.containsHeader("Set-Cookie")) {
PropertyManager.setSessionID(extractSessionId(response.getHeaders("Set-Cookie")[0].getValue()));
}
return responseBody;
} catch(UnsupportedEncodingException usee) {
usee.printStackTrace();
} catch(ClientProtocolException cpe) {
cpe.printStackTrace();
} catch(IOException ioe) {
ioe.printStackTrace();
}
return null;
}
Actually this method is working fine when my device is connected to Internet through WiFi and 3G. But just for one device (Sony Ericson - Xperia) when it is connected to 3G (WiFi is ok) server sends nonsense response.
I need to see my connection address with its detail parameter. I wrote this code:
Log.i("Requestd Connection", httppost.getURI().toString());
However, I could see just my URL without any nameValuePairs parameters.
Any suggestion would be appreciated.
I'm currently trying to send data via POST to a server, and the server is handling the data and appends it to a JSON file. I'm currently getting a 422 error and I've been receiving it for a while now. My question is: How do I receive that JSON error itself in Java so that I can see what the error is. All I'm seeing is a HttpResponseException and it doesn't give me anything else. Thanks for the time and help in advance.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(mPath);
// Add your data
try
{
List nameValuePairs = new ArrayList(4);
httppost.setHeader("Authorization", Base64.encodeToString(new StringBuilder(bundleId).append(":").append(apiKey).toString().getBytes("UTF-8"), Base64.URL_SAFE|Base64.NO_WRAP));
nameValuePairs.add(new BasicNameValuePair("state", "CA"));
nameValuePairs.add(new BasicNameValuePair("city", "AndDev is Cool!"));
nameValuePairs.add(new BasicNameValuePair("body", "dsads is assrawstjljalsdfljasldflkasjdfjasldjflasjdflkjaslfggddsfgfdsgfdsgfdsgfdsgfdsgfdsgfdsgfdsgfdsgfdsgfdsgfdsgfddjflaskjdfkasjdlfkjasldfkjalskdjfajasldfkasdlfjasljdflajsdfjasdjflaskjdflaksjdfljasldfkjasljdflajsasdlfkjasldfkjlas!"));
nameValuePairs.add(new BasicNameValuePair("title", "dsaghhhe fd!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
//HttpResponse response = httpclient.execute(httppost);
//int status = response.getStatusLine().getStatusCode();
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httppost, responseHandler);
Log.v(TAG, "response: " + responseBody);
//JSONObject response = new JSONObject(responseBody);
int f = 0;
}
catch(HttpResponseException e)
{
Log.e(TAG, e.getLocalizedMessage());
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
you may send your parameters more conveniently by using predefined Json class as below:
String jsonParam = null;
try{
JSONObject param = new JSONObject();
param.put("state", "CA");
param.put("city", "AndDev is Cool!");
//and so on with other parameters
jsonParam = param.toString();
}
catch (Exception e) {
// TODO: handle exception
}
and set the post entity as:
if(jsonParam != null)
httppost.setEntity(new StringEntity(jsonParam, HTTP.UTF_8));
422 error says: Unprocessable Entity - The request was well-formed but was unable to be followed due to semantic errors
You got a problem with UrlEncodedFormEntity
In my application I'm using outpost method to send data to web service which is in .net.Now when i send string with spaces or special characters it takes the string with e.g. test string would display like test+string.
I'm using httpDefaultClient with nameValuePair to send data...i've used UrlEncode function to encode my string but still result is the same...
Please help me...
Here is my code
//web service call
HttpClient client=new DefaultHttpClient();
HttpPost request = new HttpPost();
request.setURI(new URI(url2));
List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
// nameValuePairs.add(new BasicNameValuePair("CreateHotSpots", value))
nameValuePairs.add(new BasicNameValuePair("sKey",""+globalClass.getUser_key()));
nameValuePairs.add(new BasicNameValuePair("sLAKE",encodedlakename));
if(!(DragAndDropPinActivity.point.isEmpty())){
nameValuePairs.add(new BasicNameValuePair("sLAT",""+DragAndDropPinActivity.point.get(0)));
nameValuePairs.add(new BasicNameValuePair("sLon",""+DragAndDropPinActivity.point.get(1)));
}
else
{
url2=url2.concat("&sLAT="+""+myLatitude);
url2=url2.concat("&sLon="+""+myLongitude);
}
nameValuePairs.add(new BasicNameValuePair("sDesc",encodedDesc));
nameValuePairs.add(new BasicNameValuePair("sSpeciesofFish",encodedFishSpecies));
nameValuePairs.add(new BasicNameValuePair("sBaitUsed",encodedbUsed));
nameValuePairs.add(new BasicNameValuePair("sWeatherInformation",encodedwInfo));
nameValuePairs.add(new BasicNameValuePair("season",encodedlseason));
nameValuePairs.add(new BasicNameValuePair("sDesc",""+encodedDesc));
if(share)
{
nameValuePairs.add(new BasicNameValuePair("sShareInfo","true"));
}
else
{
nameValuePairs.add(new BasicNameValuePair("sShareInfo","false"));
}
Log.e("name value pairs",""+nameValuePairs.toString());
UrlEncodedFormEntity entity_st=new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
request.setEntity(entity_st);
HttpResponse response = client.execute(request);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
Responce=EntityUtils.toString(resEntity);
Log.i("responce ======",""+Responce);
}
}
catch (Exception e) {
e.printStackTrace();
String message=e.getMessage();
Log.e("meaasge with erroe",message);
}
return Responce;
}
step 1
You have specified a "UTF-8" encoding in
UrlEncodedFormEntity entity_st=new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
Try to change it to
UrlEncodedFormEntity entity_st=new UrlEncodedFormEntity(nameValuePairs);
step 2
I noticed that the strings you are adding to the request are called encodeXXXX
Does this mean that you are encoding them before adding them to the ValuePairs?
If so, stop doing this and keep them as normal strings.
I am trying to retrieve a response from my server which ultimately redirects to a URL after post data is submitted. I figured I could retrieve the url response that the server redirects with , with the code below but every time I do i get a fatal error out of memory. I was wondering what may cause this error if any one had any ideas of why I am getting this error or if this is an incorrect way? I have tested I am sending the data correctly to the server but keep getting this error. here is my code.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("token", token));
nameValuePairs.add(new BasicNameValuePair("key", "test"));
nameValuePairs.add(new BasicNameValuePair("mac", "test"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
ResponseHandler<String> responseHandler=new BasicResponseHandler();
String responseBody = httpclient.execute(httppost, responseHandler);
Log.d("URL", responseBody);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}