PhuongFuk
PhuongFuk

Reputation: 23

How to call and get json webservice?

I have a problem, it's when i tried get response, my code was be stoped.

public String JCon() {

    StringBuilder strcon = new StringBuilder();
    strcon.append("{");
    strcon.append(" \"username\":\"aaaaaaaa\",");
    strcon.append("\"password\":\"1\",");
    strcon.append("\"device_type\":\"ANDROID\",");
    strcon.append("\"device_token\":\"1234567890\"");
    strcon.append("}");


    HttpClient httpC = new DefaultHttpClient();
    HttpPost httpP = new HttpPost(
            "http://203.162.10.112:8080/ezcheck/rest/user/login");
    JSONObject jcon;
    try {
        jcon = new JSONObject(strcon.toString());
        httpP.setEntity(new StringEntity(jcon.toString()));

        // error in here
        HttpResponse httpR = httpC.execute(httpP);

    } catch (JSONException e1) {
        return "Can't creat Json";
    } catch (UnsupportedEncodingException e) {
        return "UnsupportedEncodingException";
    } catch (ClientProtocolException e) {
        return "ClientProtocolException";
    } catch (IOException e) {
        return "IOException";
    }
}

this is code i try to test connect and get response from webservice. I can't get any exception, so i can't understand what is problem, i'm just know my problem is at HttpResponse httpR = httpC.execute(httpP);

Upvotes: 0

Views: 76

Answers (2)

user5010376
user5010376

Reputation: 40

You can use my code :

private  class get_txt_from_url extends AsyncTask<String , Void , String>{
        final String TAG = "get_txt_from_url";


        @Override
        protected String doInBackground(String... params) {

            InputStream is = null;
            BufferedReader reader = null;
            StringBuilder string_builder = null;
            String line ="";


            try {
                HttpClient hc = new DefaultHttpClient();
                HttpPost hp = new HttpPost(params[0]);
                HttpResponse hr = hc.execute(hp);
                HttpEntity htity = hr.getEntity();
                is = htity.getContent();
            } catch (IOException e) {
               Log.d(TAG , "ZERO");
            }

            try {
                reader = new BufferedReader(new InputStreamReader(is , "utf-8") , 8);
                string_builder = new StringBuilder();
            } catch (UnsupportedEncodingException e) {
                Log.d(TAG, "ONE");
            }


            try {
                while((line = reader.readLine()) != null){
                    string_builder.append(line+"");
                }
            } catch (IOException e) {
                Log.d(TAG, "line");
            }


            return string_builder.toString();
        }

        @Override
        protected void onPostExecute(String s) {

            Log.d(TAG , s);

        }
    }

AND ... to use the method u need to do this:

new get_txt_from_url().execute(the url String/*Example:  http://www.google.com*/);

and You will see it in the log cat.

Upvotes: 0

Desmond Yao
Desmond Yao

Reputation: 565

You can't open a http or otherwise net connection on Main Thread, it would cause a fatal exception NetworkOnMainThreadException. If you can't observe any exception in IDE, open command line and type:

adb logcat "*:E"

You would see the exception.

Try this in your code:

new Thread(new Runnable(){
    JCon();
});

Then it would be ok, but I suggest using Handler or Asynctask instead of Thread.

Upvotes: 1

Related Questions