[英]Android JSON Data Posting results in IO Exception Always
I have a server and I use following Link to post data in my DB 我有一台服务器,并且使用以下链接在数据库中发布数据
http://URL.php?action=insert&vhc=vehiclenumber&mobile=mobilenumberoftheperson&time=currenttime http://URL.php?action = insert&vhc = vehiclenumber&mobile = mobilepersonofperson&time =当前时间
To keep record of vehicle entering time, Now I want to post same Data from Android for which I am using Following Code: 为了保留车辆进入时间的记录,现在我想从Android中发布我正在使用以下代码的相同数据:
Inside Button Goes this: 内部按钮转到此:
button.setOnClickListener(new View.OnClickListener() {
//Toast.makeText(getApplicationContext(),"Data is tried!", Toast.LENGTH_LONG).show();
public void onClick(View v)
{
new Thread(new Runnable() {
public void run() {
postData();
}
}).start();
}
});
And my PostData Function is defined below: 我的PostData函数定义如下:
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("url.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("vhc", "ABC124"));
nameValuePairs.add(new BasicNameValuePair("mobile", "089944440000"));
nameValuePairs.add(new BasicNameValuePair("time", "2014-12-28 22:22:52"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
message = e.getMessage();
// TODO Auto-generated catch block
} catch (IOException e) {
message= e.getMessage();
// TODO Auto-generated catch block
}
}
Now I tried to debug and found it failing at HttpResponse response = httpclient.execute(httppost); 现在我尝试调试,发现它在HttpResponse response = httpclient.execute(httppost);失败。 step and leads it to IOException, Please indicate my error to resolve the issue.
步骤并将其引导至IOException,请指出我的错误以解决此问题。 Thanks!
谢谢!
In you current solution you create a thread, but the thread execute you code sequentially and it does not wait for the response from your server. 在当前解决方案中,您创建了一个线程,但是该线程按顺序执行您的代码,并且它不等待服务器的响应。 As a result your client get a null response.
结果,您的客户得到一个空响应。
Solution : you have to use AsyncTask for retrieve/send data from/to server. 解决方案:您必须使用AsyncTask从服务器检索数据/向服务器发送数据。 Execute data retrieve part in the doBackground method of AsyncTask.
在AsyncTask的doBackground方法中执行数据检索部分。 And get your response from the postExecute method.
并从postExecute方法获得响应。 See details here
在这里查看详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.