I am creating android application, in which there is a search module where user enters the data then it send to server and server sends the output back to the user and it is displayed in form of table.
My problem is that i am getting this error
06-02 00:08:43.971 19902-20586/com.diamond.traders W/SingleClientConnManager﹕ Invalid use of SingleClientConnManager: connection still allocated.
Make sure to release the connection before allocating another one.
06-02 00:08:43.991 19902-20586/com.diamond.traders W/System.err﹕ org.apache.http.client.HttpResponseException: Not Found
06-02 00:08:43.991 19902-20586/com.diamond.traders W/System.err﹕ at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71)
06-02 00:08:43.991 19902-20586/com.diamond.traders W/System.err﹕ at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
06-02 00:08:43.991 19902-20586/com.diamond.traders W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
06-02 00:08:43.991 19902-20586/com.diamond.traders W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
06-02 00:08:43.991 19902-20586/com.diamond.traders W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
06-02 00:08:43.991 19902-20586/com.diamond.traders W/System.err﹕ at com.diamond.traders.Search_result$GetDatafromDB_Searchresult.getDataFromDB(Search_result.java:676)
06-02 00:08:43.991 19902-20586/com.diamond.traders W/System.err﹕ at com.diamond.traders.Search_result$1.run(Search_result.java:80)
06-02 00:08:43.991 19902-20586/com.diamond.traders W/System.err﹕ at java.lang.Thread.run(Thread.java:811)
06-02 00:08:43.991 19902-20586/com.diamond.traders I/System.out﹕ Exception : Not Found
06-02 00:08:43.991 19902-19902/com.diamond.traders E/log_tag﹕ Error parsing data org.json.JSONException: End of input at character 0 of
here is my networking code and here is the link to complete code. Sorry can't add here as it is very long.
public class GetDatafromDB_Searchresult {
public String getDataFromDB() {
try {
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://192.168.0.106/test/search1.php"); // make sure the url is correct.
//add your data
nameValuePairs = new ArrayList<NameValuePair>(2);
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
nameValuePairs.add(new BasicNameValuePair("carat1", strcolor1.toString().trim()));
nameValuePairs.add(new BasicNameValuePair("carat2", strcolor2.toString().trim()));
nameValuePairs.add(new BasicNameValuePair("color1", strclarity1.toString().trim()));
nameValuePairs.add(new BasicNameValuePair("color2", strclarity2.toString().trim()));
nameValuePairs.add(new BasicNameValuePair("cut1", strcut1.toString().trim()));
nameValuePairs.add(new BasicNameValuePair("cut2", strcut2.toString().trim()));
nameValuePairs.add(new BasicNameValuePair("shape1", strshape1.toString().trim()));
nameValuePairs.add(new BasicNameValuePair("shape2", strshape2.toString().trim()));
nameValuePairs.add(new BasicNameValuePair("stones", strstone.toString().trim()));
// $Edittext_value = $_POST['Edittext_value'];
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response = httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response1 = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response1);
} catch (Exception e) {
System.out.println("Exception : " + e.getMessage());
}
return response1.trim();
}
}
here is my server side code
<?php
$hostname_localhost ="localhost";
$database_localhost ="testdb";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_localhost, $localhost);
$carat1 = $_POST['carat1'];
$carat2 = $_POST['carat2'];
$color1 = $_POST['color1'];
$color2 = $_POST['color2'];
$cut1 = $_POST['cut1'];
$cut2 = $_POST['cut2'];
$shape1 = $_POST['shape1'];
$shape2 = $_POST['shape2'];
$stones = $_POST['stones'];
$query_search ="Select * from search1 where
carats = $carat1 and carats = $carat2 and
color = '$color1' or color = '$color2' and
cut = '$cut1' or cut = '$cut2' and
shape = '$shape1' or shape = '$shape2' and
stone ='$stones'";
$query_exec = mysql_query($query_search) or die(mysql_error());
while($row=mysql_fetch_assoc($query_exec))
$json_output[]=$row;
echo json_encode($json_output);
mysql_close();
?>
EDIT:
This is the output from the server.
06-02 00:02:08.229 19428-19481/com.diamond.traders I/System.out﹕ [{"id":"1","product_code":"sdfsd","shaps":"fsdfsd","pair":"fsdf","carats":"sdgfdhn","color":"gnfgng","clarity":"jmf","service":"fgnfgnf","polish":"gnfg","symetric":"nfgnfg","tables":"nfgjkrtyr","measurments":"tjrt","flourscne":"jrtjr","description":"tjrtjgmgv","certificated":"mvbmv","ccode":"bmvb","cut":"kmty","total":"yttynyt","file":"tyfjfjgjfg"},{"id":"2","product_code":"fgn","shaps":"fgmkyh","pair":"wqqfwfw","carats":"fsfsdsdf","color":"gsdfwe","clarity":"fsdfsdf","service":"sdfs","polish":"dfsd","symetric":"fsdfs","tables":"dfsdf","measurments":"sdf","flourscne":"sdfsd","description":"fsdfsdd","certificated":"sfs","ccode":"dfsd","cut":"fsdf","total":"sdf","file":"sdf"},{"id":"3","product_code":"xfcbxcb","shaps":"xcbx","pair":"cbxcb","carats":"xcb","color":"xcbx","clarity":"cxcbxc","service":"xcb","polish":"xcbx","symetric":"cbxc","tables":"bxcb","measurments":"xcbxc","flourscne":"b","description":"xcbxcb","certificated":"xcbx","ccode":"cbx","cut":"cbxc","total":"bxc","file":"b"},{"id":"4","product_code":"xcb","shaps":"xcb","pair":"xcbxc","carats":"bxc","color":"bxc","clarity":"bxc","service":"bxc","polish":"xc","symetric":"bxcb","tables":"xcbx","measurments":"cbx","flourscne":"cbx","description":"cbxcbxcb","certificated":"xcbxc","ccode":"bxc","cut":"bxcb","total":"xcb","file":"xc"}]
Looks like you're not getting return response correctly. Try something like this...
HttpResponse response = defaultHttpClient.execute(httppost);
InputStream data = response.getEntity().getContent();
I am trying to send a HTTP post request to a REST service through my android app and the client runs as an async task. Here is the client:
#Override
protected Void doInBackground(Void... params) {
String address = "http://xxx.xx.x.xxx:8080/rest/manageUser/create";
StringBuilder stringBuilder = null;
ArrayList<NameValuePair> postParameters;
try {
HttpPost httpPost = new HttpPost(address);
postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("userId", userId));
postParameters.add(new BasicNameValuePair("firstName", firstName));
postParameters.add(new BasicNameValuePair("lastName", lastName));
postParameters.add(new BasicNameValuePair("mobileNumber",
mobileNumber));
postParameters.add(new BasicNameValuePair("loginStatus",
loginStatus));
httpPost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpClient client = new DefaultHttpClient();
HttpResponse response;
stringBuilder = new StringBuilder();
response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
// System.out.println(stringBuilder);
} catch (Exception e) {
e.printStackTrace();
}
JSONObject jobj = null;
try {
jobj = new JSONObject(stringBuilder.toString());
System.out.println(jobj.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
Also when I create the client as an standalone java class it works fine. But when I use it from my Android app as an async task as above, I get the following exception:
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.example.hello.service.client.CreateUser.doInBackground(CreateUser.java:64)
at com.example.hello.service.client.CreateUser.doInBackground(CreateUser.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response
at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:93)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
... 10 more
org.json.JSONException: End of input at character 0 of
at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
at org.json.JSONTokener.nextValue(JSONTokener.java:97)
at org.json.JSONObject.<init>(JSONObject.java:155)
at org.json.JSONObject.<init>(JSONObject.java:172)
at com.example.hello.service.client.CreateUser.doInBackground(CreateUser.java:82)
at com.example.hello.service.client.CreateUser.doInBackground(CreateUser.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Can anyone suggest what could be the problem. Also in my rest service, I am recieving the data with the #FormParam . Any help would be appreciated.
I think you are using wrong HTTP method. Just check HTTP Method whether it is correct or not and just try to get json that is going as part of request body.
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.
I am getting 401 status when calling Tumblr post reblog api, other tumblr api working fine.
I followed some of Stackoverflow links but can't get ride on it.
constantly-getting-401-when-trying-to-post-to-tumblr-blog
tumblr-oauth-image-post-gives-me-401-invalid-oauth-credentials
My code snippets :
String url = String.format(REBLOG_URL, blogName);
HttpPost request = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", postId));
nameValuePairs.add(new BasicNameValuePair("reblog_key", reblogKey));
nameValuePairs.add(new BasicNameValuePair("comment", comment));
nameValuePairs.add(new BasicNameValuePair("api_key", consumerKey));
request.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumerKey, secretKey);
consumer.setTokenWithSecret(mDataStore.getAccessToken(mUserSequenceId),
mDataStore.getTokenSecret(mUserSequenceId));
consumer.sign(request);
String response = makePostRequest(request);
L.d(TAG, "postReblog Response : " + response);
Error Output :
10-21 21:48:31.851: W/DefaultRequestDirector(30729):
Authentication error: Unable to respond to any of these challenges: {}
10-21 21:48:31.851: D/PhotoAPI(30729): post response code:401 10-21
21:48:31.861: D/PhotoAPI(30729): Post response :
{"meta":{"status":401,"msg":"Not Authorized"},"response":[]}
Thanks in advance.
I'm getting exceptions when I'm trying to POST and my JSON has some international characters.
This is code that I use:
HttpPost request = new HttpPost(serviceURL + url);
request.addHeader("Authorization", "Basic " + Preferences.getAuthorizationTicket(mContext));
request.addHeader("DeviceSerialNumber", Utility.getDeviceSerialNumber(mContext));
request.addHeader("OSVersion", "Android v" + Build.VERSION.RELEASE);
StringEntity entity = new StringEntity(requestData);
entity.setContentType("application/json;charset=UTF-8");
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
request.setEntity(entity);
ResponseHandler<String> handler = new BasicResponseHandler();
response.Body = mHttpClient.execute(request, handler);
response.Code = HttpURLConnection.HTTP_OK;
response.Message = "OK";
And this is error I'm getting:
org.apache.http.client.HttpResponseException:
Bad Request 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:657)
at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
at
com.idatt.common.AsyncProcessor.processPOST(AsyncProcessor.java:550)
at
com.idatt.common.AsyncProcessor.PostMail(AsyncProcessor.java:367)
at
com.idatt.common.AsyncProcessor.doInBackground(AsyncProcessor.java:120)
at
com.idatt.common.AsyncProcessor.doInBackground(AsyncProcessor.java:28)
at
android.os.AsyncTask$2.call(AsyncTask.java:185)
at
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at
java.util.concurrent.FutureTask.run(FutureTask.java:137)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
at
java.lang.Thread.run(Thread.java:1096)
When requestData doesn't have any international (russian, polish, etc) letters than it works fine. What do I miss? Or better yet how do I get traffic from emulator captured in Fiddler or something?
I believe you should specify UTF-8 charset when you create StringEntity:
StringEntity entity = new StringEntity(requestData,"utf-8");
http://developer.android.com/reference/org/apache/http/entity/StringEntity.html#StringEntity(java.lang.String, java.lang.String)