我正在使用volley从服务器获取Json数据。这是一种检测语言API,它接受文本和API密钥作为参数。当我发送请求时,来自API的响应是“密钥无效”。我已经在邮递员上检查了它,钥匙工作正常。
这是我的代码。
public void detectlanguage(Context context, final String str) {
String url = String.format("http://ws.detectlanguage.com/0.2/detect");
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, null,
new com.android.volley.Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
res = response.toString();
try {
JSONObject jsonObject = new JSONObject(res);
Log.i((TAG), jsonObject.toString());
jsonArray = jsonObject.getJSONObject("data").getJSONArray("detections");
string = jsonArray.getJSONObject(0).getString("language");
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(TakePicture.this, string, Toast.LENGTH_LONG).show();
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>(2);
params.put("q", str);
params.put("key", key);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "form-data;boundary=" + BOUNDARY);
return headers;
}
};
AppController.getInstance().addToRequestQueue(jsonObjectRequest, TAG);
}
当我手动添加数据时,代码工作,但它没有以这种方式工作。可能是什么问题?
答案 0 :(得分:0)
public void detectlanguage(Context context, final String str) {
String url = String.format("http://ws.detectlanguage.com/0.2/detect");
JSONObject jsonObject = new JSONObject();
try{
jsonObject.put("first_name", "S");
jsonObject.put("last_name", "K");
}
catch(Exception e)
{}
//3rd Parms not null if your have parms
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
new com.android.volley.Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
res = response.toString();
try {
JSONObject jsonObject = new JSONObject(res);
Log.i((TAG), jsonObject.toString());
jsonArray = jsonObject.getJSONObject("data").getJSONArray("detections");
string = jsonArray.getJSONObject(0).getString("language");
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(TakePicture.this, string, Toast.LENGTH_LONG).show();
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>(2);
params.put("q", str);
params.put("key", key);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "form-data;boundary=" + BOUNDARY);
return headers;
}
};
AppController.getInstance().addToRequestQueue(jsonObjectRequest, TAG);
}