猿问

如何通过Android请求发送JSON对象?

如何通过Android请求发送JSON对象?

我想发送以下JSON文本

{"Email":"aaa@tbbb.com","Password":"123456"}

并读取响应。我知道怎么读JSON。问题是上面的JSON对象必须以变量名发送jason.

我怎样才能在Android上做到这一点呢?创建请求对象、设置内容标题等步骤有哪些。


三国纷争
浏览 819回答 3
3回答

慕村9548890

Android没有发送和接收HTTP的特殊代码,您可以使用标准Java代码。我建议使用Android附带的ApacheHTTP客户机。下面是我用来发送HTTPPOST的代码片段。我不明白在名为“Jason”的变量中发送对象与任何事情有什么关系。如果您不确定服务器到底想要什么,请考虑编写一个测试程序,将各种字符串发送到服务器,直到您知道它需要采用何种格式为止。int TIMEOUT_MILLISEC = 10000;  // = 10 secondsString postMessage="{}"; //HERE_YOUR_POST_STRING.HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); HttpClient client = new DefaultHttpClient(httpParams);HttpPost request = new HttpPost(serverUrl);request.setEntity(new ByteArrayEntity(     postMessage.toString().getBytes("UTF8")));HttpResponse response = client.execute(request);

慕标5832272

如果使用ApacheHTTP客户端,从Android发送JSON对象很容易。下面是一个关于如何实现的代码示例。您应该为网络活动创建一个新线程,以避免锁定UI线程。    protected void sendJson(final String email, final String pwd) {         Thread t = new Thread() {             public void run() {                 Looper.prepare(); //For Preparing Message Pool for the child Thread                 HttpClient client = new DefaultHttpClient();                 HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit                 HttpResponse response;                 JSONObject json = new JSONObject();                 try {                     HttpPost post = new HttpPost(URL);                     json.put("email", email);                     json.put("password", pwd);                     StringEntity se = new StringEntity( json.toString());                       se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));                     post.setEntity(se);                     response = client.execute(post);                     /*Checking response */                     if(response!=null){                         InputStream in = response.getEntity().getContent(); //Get the data in the entity                     }                 } catch(Exception e) {                     e.printStackTrace();                     createDialog("Error", "Cannot Estabilish Connection");                 }                 Looper.loop(); //Loop in the message queue             }         };         t.start();           }你也可以用谷歌gson发送和检索JSON。

Cats萌萌

public void postData(String url,JSONObject obj) {     // Create a new HttpClient and Post Header     HttpParams myParams = new BasicHttpParams();     HttpConnectionParams.setConnectionTimeout(myParams, 10000);     HttpConnectionParams.setSoTimeout(myParams, 10000);     HttpClient httpclient = new DefaultHttpClient(myParams );     String json=obj.toString();     try {         HttpPost httppost = new HttpPost(url.toString());         httppost.setHeader("Content-type", "application/json");         StringEntity se = new StringEntity(obj.toString());          se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));         httppost.setEntity(se);          HttpResponse response = httpclient.execute(httppost);         String temp = EntityUtils.toString(response.getEntity());         Log.i("tag", temp);     } catch (ClientProtocolException e) {     } catch (IOException e) {     }}
随时随地看视频慕课网APP

相关分类

Android
我要回答