我正在使用代码,尝试将参数发送到服务器时收到此错误。
com.android.volley.VolleyError: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
然后我尝试将Looper.prepare()添加到代码中,现在当我按下按钮时应用程序崩溃。我正在使用QR阅读器获取textview,然后将其发送回服务器的某些部分,然后程序的其他部分也将发送给服务器。这只是我发送数据时的一部分。我也正在获取位置,并使用了微调框和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);
我应该在哪里添加那行代码?
答案 0 :(得分:0)
无法在未调用Looper.prepare()异常的线程内创建处理程序的可能原因是尝试从后台线程执行与UI相关的操作。
在您的情况下,您不应该在getParams()
方法中显示吐司,因为它可能是从凌空工作线程中调用的
答案 1 :(得分:0)
onResponse和onErrorResponse在UI线程之外执行。