我正在使用带有二维码阅读器的应用程序工作。我正在尝试使用凌空库向服务器发送http发布请求。但是,它直接跳到该行的错误例外。这是我的代码,
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(QRCODEPAGE1.this, "Started", Toast.LENGTH_SHORT).show();
RequestQueue mRequestQueue = Volley.newRequestQueue(QRCODEPAGE1.this);
String url = "http://api-dev.eportfolioapi.com/api/sensor/plan";
// String url = "http://localhost:5000/api/sensor/updateDeviceStage/5b3e6e788030580019ebc333";
Cache cache = new DiskBasedCache(getCacheDir(), 1024*1024);
Network network = new BasicNetwork(new HurlStack());
//mRequestQueue = new RequestQueue(cache, network);
Toast.makeText(QRCODEPAGE1.this, "DONE", Toast.LENGTH_SHORT).show();
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
tvresult.setText("Response is:" + response.substring(0, 2000));
Toast.makeText(QRCODEPAGE1.this, "YOU DID IT", Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
tvresult.setText("That didn't work!");
Toast.makeText(QRCODEPAGE1.this, "Wish harder", Toast.LENGTH_SHORT).show();
}
});
//mRequestQueue.add(stringRequest);
stringRequest.setTag(TAG);
mRequestQueue.add(stringRequest);
请注意,我在应用程序gradle和清单中添加了必要的内容。
这就是我的代码现在的样子...
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);
我收到一条错误消息,说我需要添加Looper.prepare()。我应该放在哪里?
答案 0 :(得分:0)
您正尝试发出类似Request.Method.GET
的get请求,而您的api需要类似Request.Method.POST
的post请求,同样在检查api时,您还需要传递这些参数{{1} }
userId, customerId, projectId, taskId, name and prTypeId
发出这样的请求:
postman result
在StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("userId", "");
params.put("customerId", "");
params.put("projectId", "");
params.put("taskId", "");
params.put("name", "");
params.put("prTypeId", "");
return params;
}
};
内传递所有必需的参数和相应的值