无法连接到HTTP服务器并在android中获取json字符串

时间:2015-04-01 11:31:39

标签: android json http http-post httpclient

我正在尝试获取Json字符串,网址:http://ip-api.com/json。 我尝试获取json字符串时遇到IO异常。

package www.howdy.com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;



public class ServerHandler {


    static InputStream is = null;
    static JSONObject jobj = null;
    static String json = "";

    //this method returns json object.
    public static JSONObject makeHttpRequest(String url){
    //http client helps to send and receive data
    DefaultHttpClient httpclient = new DefaultHttpClient();
    //our request method is post
    HttpPost httppost = new HttpPost(url);
    try {
    //get the response
    HttpResponse httpresponse = httpclient.execute(httppost);
    HttpEntity httpentity = httpresponse.getEntity();
    // get the content and store it into inputstream object.
    is = httpentity.getContent();
    } catch (ClientProtocolException e) {
        Log.d("error++++++++++++++++++++++++++++", "client protocol exception");
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Log.d("error++++++++++++++++++++++++++++", "io exception ");
    }
    try {
    //convert byte-stream to character-stream.
    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    try {
    while((line = reader.readLine())!=null){
    sb.append(line+"\n");
    }
    //close the input stream
    is.close();
    json = sb.toString();
    try {
    jobj = new JSONObject(json);
    } catch (JSONException e) {
    // TODO Auto-generated catch block
        Log.d("error++++++++++++++++++++++++++++", "json exception");
    e.printStackTrace();
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Log.d("error++++++++++++++++++++++++++++", "io 2 exception");
    }
    } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Log.d("error++++++++++++++++++++++++++++", "UnsupportedEncodingException e");
    }
    return jobj;
    }
}

我的主要活动是使用异步任务,

private class Start extends AsyncTask<Void, Void, Void> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

                txt.setText("");

            }



            protected Void doInBackground(Void... arg0){

                //
                j = ServerHandler.makeHttpRequest(url);

                try {
                    ab = j.getString("as");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(getApplicationContext(), "json exception", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();

                }
                return null;




            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);

               txt.setText(ab);
            }


        }

其中j是JSONObject,ab是String,txt是TextView。 无论何时我运行此代码,我都会在logcat中获得IO异常。

Log.d("error++++++++++++++++++++++++++++", "io exception ");

0 个答案:

没有答案