我在我的Android应用程序中有一个活动,我在按钮点击时将数据发送到服务器。但是当我点击按钮时,我得到了java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
我在谷歌上搜索但没有得到解决方案。
以下是我的代码:
try
{
HttpAsyncRegUser httpasync = new HttpAsyncRegUser();
urlforpurchase = "http://"+ Constants.ip +"/venuscmsapp/services/purchase/insertpurchasedetails";
Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_LONG).show();
httpasync.execute(new String[] { urlforpurchase });
}
catch(Exception excp){
Log.e("Error fetching data : ", excp.getMessage());
}
Toast.makeText(getApplicationContext(), "hii", Toast.LENGTH_LONG).show();
}
private class HttpAsyncRegUser extends AsyncTask<String, Void, String> {
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Post", Toast.LENGTH_LONG).show();
super.onPostExecute(result);
}
@Override
protected String doInBackground(String... params) {
String str = null;
try {
//Toast.makeText(getApplicationContext(), "in HttpAsyncRegUser", Toast.LENGTH_LONG).show();
HttpClient httpclient = new DefaultHttpClient();
String json;
HttpPost httpPost = new HttpPost(urlforpurchase);
JSONObject puchaseJson = new JSONObject();
puchaseJson.put("productId", ITEM_SKU);
puchaseJson.put("userId", userId);
puchaseJson.put("productprice", price);
puchaseJson.put("productStartDate", productStartDate);
puchaseJson.put("productEndDate", productEndDate);
puchaseJson.put("paymentStatus",1);
puchaseJson.put("paymentDate", paymentDate);
puchaseJson.put("productType","SONG");
puchaseJson.put("status",1);
json = puchaseJson.toString();
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> "+json);
StringEntity se = new StringEntity(json);
System.out.println("========================= "+se);
Toast.makeText(getApplicationContext(), "in application/json", Toast.LENGTH_LONG).show();
Log.e("post", "in application/json");
httpPost.setEntity(se);
httpPost.setHeader("Accept","application/json");
httpPost.setHeader("Content-Type", "application/json");
Toast.makeText(getApplicationContext(), "in application/json", Toast.LENGTH_LONG).show();
HttpResponse httpResponse = httpclient.execute(httpPost);
Toast.makeText(getApplicationContext(), "in httpResponse", Toast.LENGTH_LONG).show();
str = inputStreamToString(httpResponse.getEntity().getContent()).toString();
Toast.makeText(getApplicationContext(), "in last", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
//Toast.makeText(getApplicationContext(), ""+e, Toast.LENGTH_LONG).show();
Log.e(" my Error", ""+e);
}
return str;
}
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return total;
}
}
以下是我的logcat输出:
09-18 12:03:40.851: W/System.err(1742): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-18 12:03:40.851: W/System.err(1742): at android.os.Handler.<init>(Handler.java:121)
09-18 12:03:40.851: W/System.err(1742): at android.widget.Toast.<init>(Toast.java:68)
09-18 12:03:40.851: W/System.err(1742): at android.widget.Toast.makeText(Toast.java:231)
09-18 12:03:40.851: W/System.err(1742): at com.venus.browse.BrowseSongListActivity$HttpAsyncRegUser.doInBackground(BrowseSongListActivity.java:1005)
09-18 12:03:40.851: W/System.err(1742): at com.venus.browse.BrowseSongListActivity$HttpAsyncRegUser.doInBackground(BrowseSongListActivity.java:1)
09-18 12:03:40.851: W/System.err(1742): at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-18 12:03:40.851: W/System.err(1742): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-18 12:03:40.861: W/System.err(1742): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-18 12:03:40.861: W/System.err(1742): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-18 12:03:40.861: W/System.err(1742): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-18 12:03:40.861: W/System.err(1742): at java.lang.Thread.run(Thread.java:1096)
09-18 12:03:40.861: E/my Error(1742): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
答案 0 :(得分:1)
Toast消息显示在UI上,因此要从线程中显示它,我们必须将其放入UIThread。
替换Toast
功能内的每条doInBackground
条消息,如下所示
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "in application/json", Toast.LENGTH_LONG).show();
}
});
答案 1 :(得分:0)
您无法在Toast
的{{1}}方法中使用doInBackground()
。
Toast实际上与UI交互,AsyncTask
方法在后台运行(后台线程),并且不会影响UI。
这就是您收到此错误的原因。
doInBackground()
答案 2 :(得分:0)
只需从doInBackground()
方法
Toast.makeText(getApplicationContext(), "in application/json", Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "in httpResponse", Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "in last", Toast.LENGTH_LONG).show();
您无法使用该方法更新您的用户界面。
在doInBackground()
中,您只能执行后台处理和网络操作。