`

using HttpClient and sending json data to RESTful server in adroind

 
阅读更多

1.send a json object by parameter to request the RESTful server,as flowing code:

package tuo.json;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
 
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import com.guc.android.nurse.activity.R.string;
 
/*
 * this class  call RESTful WCF,and return json data
 */
public class JsonTruck {
 
    public String getJsonString(String wcfUrl) throws Exception {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI(wcfUrl));
        HttpResponse response = httpClient.execute(request);
        return parseHttpResponse(response);
    }
     
    public  String parseHttpResponse(HttpResponse response) throws Exception {
        String jsonString="";
        int status = response.getStatusLine().getStatusCode();
        if (status == 200) {
            BufferedReader bReader = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = bReader.readLine()) != null) {
                sb.append(line + NL);
            }
            jsonString = sb.toString();
            bReader.close();
        }
        return jsonString;
         
    }
     
    public JSONObject getJson(String wcfUrl,String jsonObjName) throws Exception {
        String jsonString=getJsonString(wcfUrl);
        JSONObject jsonObject = new JSONObject(jsonString);
        return jsonObject.getJSONObject(jsonObjName);
    }
     
    public JSONArray getJsons(String wcfUrl,String jsonObjName) throws Exception{
        JSONObject jsonObject=new JSONObject(getJsonString(wcfUrl));
        return jsonObject.getJSONArray(jsonObjName);
         
    }
     
    public String doPost(String wcfUrl,JSONObject jsonObject) throws Exception {
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response;
        HttpPost post=new HttpPost();
        HttpEntity httpEntity;
        StringEntity stringEntity=new StringEntity(jsonObject.toString());
        stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httpEntity=stringEntity;
        post.setEntity(httpEntity);
        post.setURI(new URI(wcfUrl));
        post.setHeader("Content-type", "application/json");
        response=httpClient.execute(post);
        return parseHttpResponse(response);
    }
}

2.Test in Activity and fill a json object

package com.guc.android.nurse.activity;
 
import org.json.JSONObject;
 
import com.guc.android.nurse.comm.JsonTruck;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
 
public class Test extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        setListener();
    }
 
    void setListener(){
        Button updateBtn=(Button)findViewById(R.id.post_update);
        Button createButton=(Button)findViewById(R.id.post_create);
        updateBtn.setOnClickListener(new UpdateListener());
        createButton.setOnClickListener(new CreateListener());
    }
     
    class UpdateListener implements OnClickListener{
 
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String wcfUrl="http://192.168.0.110:8082/Nurse/SubmitRecord";
            String message="";
            JSONObject jsonObject=new JSONObject();
            JSONObject obj=new JSONObject();
             
            try {
                obj.put("his_account","00000");
                obj.put("timebh","1");
                obj.put("patientNo","*001201");
                obj.put("admissTimes","1");
                obj.put("position","1");
                obj.put("piesis_x","120");
                obj.put("piesis_y","80");
                obj.put("respiration","1");
                obj.put("temperature","1");
                obj.put("pulse","1");
                obj.put("liquidinput","1");
                obj.put("urea","1");
                obj.put("bowels","1");
                obj.put("otheroutput","1");
                jsonObject.put("record", obj);
                message=new JsonTruck().doPost(wcfUrl, jsonObject);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Toast.makeText(Test.this, e.getMessage(), 5000).show();
            }
             
            Toast.makeText(Test.this, message, 5000).show();
        }
         
    }
    class CreateListener implements OnClickListener{
 
        @Override
        public void onClick(View v) {
            Toast.makeText(Test.this, "you click create nutton", 5000).show();
             
        }
         
    }
}

the RESRful server client(WCF),you can reference previous article.

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics