[英]my code not working for sending data to php
I used JSON for sending data to php from android. 我使用JSON从Android向php发送数据。 For this I used the code as below
为此,我使用了如下代码
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://futuretime.in/reg.php");
httppost.setHeader("content-type", "application/json");
JSONObject dataJson = new JSONObject();
dataJson.put("password", password);
dataJson.put("number", Integer.parseInt("5556"));
StringEntity entity = new StringEntity(dataJson.toString());
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
text=response.toString();
when i excuted this i am getting a message like: org.apache.http.message.basichttpresponse 4fb06e5e 当我对此进行谴责时,我收到一条消息,例如:org.apache.http.message.basichttpresponse 4fb06e5e
Try this 尝试这个
text = EntityUtils.toString(response.getEntity());
Reference: EntityUtils Android 参考: EntityUtils Android
你应该打电话给response.getEntity().getContent()
Try this just pass your url in this method 试试这个只是通过这种方法传递您的网址
public void getUrlData(String url)
{
String result="";
//Making HTTP request
try
{
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 30000);
HttpConnectionParams.setSoTimeout(httpParameters, 30000);
//defaultHttpClient
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
//httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
inputStream.close();
result = sb.toString();
}
catch (Exception e)
{
url_error = 1;
System.out.println(e.toString());
}
}
And afterwards retrieve your data from String result, you can put result inside log to known the data 然后从String结果中检索数据,您可以将结果放入日志中以获取已知数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.