我使用下面的代码。在下面的代码中" str"是JSONArray转换为字符串。我必须通过这个" str"到网址
String serviceUrl = "http://172.16.10.64:8080/plugleadservices/rest/feedbackmanagement/feedbacknew";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("feedbackjson", str) );
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serviceUrl);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
StringEntity entity = new StringEntity(jsonAr.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String json_str = EntityUtils.toString(httpEntity);
我的问题是:如何将JsonArray发布到URL并获取JSON对象响应?
提前致谢...
答案 0 :(得分:1)
编辑2: 像这样使用Volley:
StringRequest req = new StringRequest(Request.Method.POST,urlJsonArry,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("", response.toString());
try
{
// your response in JsonObject
JSONObject jsonObject = new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("", "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("feedbackjson",str);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Accept", "application/json");
headers.put("Content-Type", "application/json; charset=UTF-8");
return headers;
}
};
req.setRetryPolicy(new DefaultRetryPolicy(60000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Adding request to request queue
Volley.newRequestQueue(activity).add(req);
将此添加到build.gradle:
compile 'com.mcxiaoke.volley:library:1.0.19'
或者如果您使用的是eclipse:请在此处下载volley.jar
编辑:
修复这样的导入:
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;