我正在使用我的代码,当我试图将我的参数发送到服务器时,我得到了这个错误。
com.android.volley.VolleyError: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()然后我尝试将Looper.prepare()添加到代码中,现在当我点击按钮时,应用程序崩溃了。我使用QR阅读器获取文本视图,然后发送回服务器用于某些部分,然后还有我的程序的其他部分。这正是我发送数据时的部分。我还在获取位置,并使用微调器和edittext字段。
这是我的代码。
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(QRCODEPAGE1.this, "Make it work", Toast.LENGTH_SHORT).show();
// Intent meow = new Intent(QRCODEPAGE1.this, QRLASTPAGE.class);
// startActivity(meow);
Toast.makeText(QRCODEPAGE1.this, "Started",
Toast.LENGTH_SHORT).show();
RequestQueue mRequestQueue =
Volley.newRequestQueue(QRCODEPAGE1.this);
String url = "http://api-
dev.eportfolioapi.com/api/sensor/install";
Cache cache = new DiskBasedCache(getCacheDir(), 1024*1024);
Network network = new BasicNetwork(new HurlStack());
//Looper.prepare();
//mRequestQueue = new RequestQueue(cache, network);
Toast.makeText(QRCODEPAGE1.this, "DONE",
Toast.LENGTH_SHORT).show();
StringRequest request = new StringRequest(Request.Method.POST,
url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(QRCODEPAGE1.this, "you passed", Toast.LENGTH_SHORT).show();
System.out.println(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Toast.makeText(QRCODEPAGE1.this, "you failed", Toast.LENGTH_SHORT).show();
Toast.makeText(QRCODEPAGE1.this, String.valueOf(error),
Toast.LENGTH_LONG).show();
Log.e("Volley ERROR: ", error.toString());
//Looper.prepare();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Toast.makeText(QRCODEPAGE1.this, "Params", Toast.LENGTH_SHORT).show();
Map<String, String> params = new HashMap<String, String>();
params.put("userId", tvresult.getText().toString());
Toast.makeText(QRCODEPAGE1.this, "you passed userid",
Toast.LENGTH_SHORT).show();
params.put("customerId", custID.toString());
Toast.makeText(QRCODEPAGE1.this, "you passed custid",
Toast.LENGTH_SHORT).show();
params.put("sensorId", tvresult.getText().toString());
Toast.makeText(QRCODEPAGE1.this, "sensorid",
Toast.LENGTH_SHORT).show();
params.put("projectId", tvresult.getText().toString());
Toast.makeText(QRCODEPAGE1.this, "you passed proid",
Toast.LENGTH_SHORT).show();
params.put("sensorVendorID", tvresult.getText().toString());
params.put("sensorGatewayId", tvresult.getText().toString());
// params.put("qrCodeUrl", );
params.put("lat", textLat.getText().toString());
Toast.makeText(QRCODEPAGE1.this, "lat", Toast.LENGTH_SHORT).show();
params.put("long", textLong.getText().toString());
Toast.makeText(QRCODEPAGE1.this, "long", Toast.LENGTH_SHORT).show();
params.put("locationWithinFacility", descprit.getText().toString());
return params;
}
};
request.setTag(TAG);
mRequestQueue.add(request);我应该在哪里添加这行代码?
发布于 2018-07-11 23:56:02
未调用Looper.prepare()异常的线程内无法创建处理程序的可能原因是试图从后台线程执行与UI相关的操作。
在您的示例中,您不应该在getParams()方法中显示toast,因为它可能是从Volley worker线程调用的
发布于 2018-07-11 23:59:55
onResponse和onErrorResponse在UI线程外部执行。
https://stackoverflow.com/questions/51289021
复制相似问题