如何将JSON对象发布到api并使用Volley在Android中接收原始文本
我是Volley的新手。 我试图在我的Android应用程序中使用Volley编码更改已弃用的HttpClient和HttpPost编码。 我从这里学到了JsonParsing,并能够在我的代码中实现它。 该代码告诉如何解析来自服务器的响应,并根据具体情况读取json对象或数组。
任何人都可以告诉如何将Json对象发送到服务器,并将其保存到数据库中,然后接收原始数据。 解决方案或甚至是类似代码的链接将不胜感激。 我搜索了很多,但都是徒劳的
我想将下面的代码更改为等价的。
private void saveToSite() {
try{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://clctn.mysite.com/data/checklog.php");
JSONObject json = new JSONObject();
json.put("name", ed_name.getText().toString());
json.put("address", ed_address.getText().toString());
json.put("contact", ed_contact.getText().toString());
JSONArray postjson=new JSONArray();
postjson.put(json);
// Post the data:
httppost.setHeader("json",json.toString());
httppost.getParams().setParameter("jsonpost",postjson);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
// for JSON:
if(response != null)
{
InputStream is = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}catch (ClientProtocolException e) {
Log.e("cpe1 ", e.toString());
} catch (IOException e) {
Log.e("ioe1 ", e.toString());
} catch (JSONException e){
Log.e("jsonex ", e.toString());
}
}
StringRequest request = new StringRequest(Request.Method.POST,
StaticVeriable.GET_PHOTOS, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> params = new HashMap<>();
params.put("parameter1", ""+yourjsonObject.toString(););
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(request);
}
链接地址: http://www.djcxy.com/p/29513.html
上一篇: How to post JSON object to an api and receive raw text in Android using Volley
下一篇: Getting Json data from (Local) android assets folder using retrofit