Android Json结果返回null

Android Json结果返回null,android,json,Android,Json,我是Android新手。我想从本地主机获取数据 String cus_id = etcusid.getText().toString(); new cusdataAsynTask().execute(cus_id); public class cusdataAsynTask extends AsyncTask<String, Void, Void> { @Override protected Void doInBackground(String... params)

我是Android新手。我想从本地主机获取数据

String cus_id = etcusid.getText().toString();
new cusdataAsynTask().execute(cus_id);

public class cusdataAsynTask extends AsyncTask<String, Void, Void> {
@Override
        protected Void doInBackground(String... params) {
            String nvpcusid = params[0];
            List<NameValuePair> arg = new ArrayList<NameValuePair>();
            Log.e("nvpcusid: ", "> " + nvpcusid);

            arg.add(new BasicNameValuePair("cus_id", nvpcusid));
ServiceHandler jsonParser = new ServiceHandler();
            String json = jsonParser.makeServiceCall(URL_CUSTOMERDATA,
                    ServiceHandler.GET,arg);
            Log.e("Response: ", "> " + json);
}
}
String cus_id=etcusid.getText().toString();
新建cusdataAsynTask().execute(cus_id);
公共类cusdataAsynTask扩展异步任务{
@凌驾
受保护的Void doInBackground(字符串…参数){
字符串nvpcusid=params[0];
List arg=new ArrayList();
Log.e(“nvpcusid:”,“>”+nvpcusid);
参数添加(新的BasicNameValuePair(“客户id”,nvpcusid));
ServiceHandler jsonParser=新ServiceHandler();
字符串json=jsonParser.makeServiceCall(URL\u CUSTOMERDATA,
获取,arg);
Log.e(“响应:”,“>”+json);
}
}
在日志中,我得到的响应为null

下面给出了服务处理程序类

public class ServiceHandler {

    static InputStream is = null;
    static String response = null;
    public final static int GET = 1;
    public final static int POST = 2;

    public ServiceHandler() {

    }

    /*
     * Making service call
     * @url - url to make request
     * @method - http request method
     * */
    public String makeServiceCall(String url, int method) {
        return this.makeServiceCall(url, method, null);
    }

    /*
     * Making service call
     * @url - url to make request
     * @method - http request method
     * @params - http request params
     * */
    public String makeServiceCall(String url, int method,
            List<NameValuePair> params) {
        try {
            // http client
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            // Checking http request method type
            if (method == POST) {
                HttpPost httpPost = new HttpPost(url);
                // adding post params
                if (params != null) {
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                }

                httpResponse = httpClient.execute(httpPost);

            } else if (method == GET) {
                // appending params to url
                if (params != null) {
                    String paramString = URLEncodedUtils
                            .format(params, "utf-8");
                    url += "?" + paramString;
                }
                HttpGet httpGet = new HttpGet(url);

                httpResponse = httpClient.execute(httpGet);

            }
            httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            response = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error: " + e.toString());
        }

        return response;

    }
公共类ServiceHandler{
静态InputStream为空;
静态字符串响应=null;
公共最终静态int GET=1;
公共最终静态int POST=2;
公共服务处理程序(){
}
/*
*拨打服务电话
*@url-发出请求的url
*@method-http请求方法
* */
公共字符串makeServiceCall(字符串url,int方法){
返回此.makeServiceCall(url,方法,null);
}
/*
*拨打服务电话
*@url-发出请求的url
*@method-http请求方法
*@params-http请求参数
* */
公共字符串makeServiceCall(字符串url,int方法,
列表参数){
试一试{
//http客户端
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpEntity HttpEntity=null;
HttpResponse HttpResponse=null;
//检查http请求方法类型
if(方法==POST){
HttpPost HttpPost=新的HttpPost(url);
//添加post参数
如果(参数!=null){
setEntity(新的UrlEncodedFormEntity(参数));
}
httpResponse=httpClient.execute(httpPost);
}else if(方法==GET){
//将参数附加到url
如果(参数!=null){
String paramString=URLEncodedUtils
.格式(参数“utf-8”);
url+=“?”+参数字符串;
}
HttpGet HttpGet=新的HttpGet(url);
httpResponse=httpClient.execute(httpGet);
}
httpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
为“UTF-8”)、8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
response=sb.toString();
}捕获(例外e){
Log.e(“缓冲区错误”,“错误:+e.toString());
}
返回响应;
}
}

我是这方面的初学者,请帮帮我

请尝试以下代码:

该类用于从服务器获取jsonObject和jsonArray:

public class GetDataJsonFromServer {


    public static JSONObject postJSONfromURL(String url,
            List<NameValuePair> params, int socketFactory, String userAgent)
            throws SSLPeerUnverifiedException {
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;
        try {
            SchemeRegistry scheme = new SchemeRegistry();
            scheme.register(new Scheme("http", PlainSocketFactory
                    .getSocketFactory(), socketFactory));

            HttpClient httpclient = new DefaultHttpClient();
            HttpProtocolParams.setUserAgent(httpclient.getParams(), userAgent);
            HttpPost httpost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httpost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "utf-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) {
        }

        try {
            jArray = new JSONObject(result);
        } catch (JSONException e) {
        }
        return jArray;
    }

    public static JSONArray mygetJSONfromURL(String url,
            List<NameValuePair> params, int socketFactory, String userAgent)
            throws SSLPeerUnverifiedException {
        InputStream is = null;
        String result = "";
        JSONArray jArray = null;
        try {
            SchemeRegistry scheme = new SchemeRegistry();
            scheme.register(new Scheme("http", PlainSocketFactory
                    .getSocketFactory(), socketFactory));

            HttpClient httpclient = new DefaultHttpClient();
            HttpProtocolParams.setUserAgent(httpclient.getParams(), userAgent);

            String paramString = URLEncodedUtils.format(params, "utf-8");

            HttpGet httpget = new HttpGet(url + paramString);
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "utf-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) {
        }

        try {
            jArray = new JSONArray(result);
        } catch (JSONException e) {
        }

        return jArray;
    }

    public static JSONObject getJSONfromURL(String url,
            List<NameValuePair> params, int socketFactory, String userAgent)
            throws SSLPeerUnverifiedException {
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;
        try {
            SchemeRegistry scheme = new SchemeRegistry();
            scheme.register(new Scheme("http", PlainSocketFactory
                    .getSocketFactory(), socketFactory));

            HttpClient httpclient = new DefaultHttpClient();
            HttpProtocolParams.setUserAgent(httpclient.getParams(), userAgent);

            String paramString = URLEncodedUtils.format(params, "utf-8");

            HttpGet httpget = new HttpGet(url + paramString);
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
        }

        // convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "utf-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) {
        }

        try {
            jArray = new JSONObject(result);
        } catch (JSONException e) {
        }

        return jArray;
    }

}
公共类GetDataJsonFromServer{
公共静态JSONObject postJSONfromURL(字符串url,
列表参数、int socketFactory、字符串userAgent)
抛出SSLPeerUnverifiedException{
InputStream=null;
字符串结果=”;
JSONObject jArray=null;
试一试{
SchemeRegistry scheme=新SchemeRegistry();
scheme.register(新方案(“http”),PlainSocketFactory
.getSocketFactory(),socketFactory));
HttpClient HttpClient=新的DefaultHttpClient();
HttpProtocolParams.setUserAgent(httpclient.getParams(),userAgent);
HttpPost HttpPost=新的HttpPost(url);
HttpResponse response=httpclient.execute(httpost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
为“utf-8”)、8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
}
试一试{
jArray=新的JSONObject(结果);
}捕获(JSONException e){
}
返回jArray;
}
公共静态JSONArray mygetJSONfromURL(字符串url,
列表参数、int socketFactory、字符串userAgent)
抛出SSLPeerUnverifiedException{
InputStream=null;
字符串结果=”;
JSONArray-jArray=null;
试一试{
SchemeRegistry scheme=新SchemeRegistry();
scheme.register(新方案(“http”),PlainSocketFactory
.getSocketFactory(),socketFactory));
HttpClient HttpClient=新的DefaultHttpClient();
HttpProtocolParams.setUserAgent(httpclient.getParams(),userAgent);
String paramString=URLEncodedUtils.format(params,“utf-8”);
HttpGet HttpGet=新的HttpGet(url+paramString);
HttpResponse response=httpclient.execute(httpget);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
是“utf-8”)
        public class cusdataAsynTask extends AsyncTask<String, Void, jsonObject> {
        @Override
                protected Void doInBackground(String... params) {
                    List<NameValuePair> listParams = new ArrayList<NameValuePair>();
                    JSONObject josn = new JSONObject();
                    String url = params[0];
                    Log.w("url", url);
                    try {
                        josn = GetDataJsonFromServer.postJSONfromURL(url, listParams,
                                80, "Bazan version for Android");
                    } catch (SSLPeerUnverifiedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    return josn;
        }

    @Override
    protected void onPostExecute(JSONObject result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        if (result != null) {
           // add your code here
        }
   }
}
private void get_valueFromServer(String php) {
String responseString = null;
try{    
HttpClient httpclient = new DefaultHttpClient();
String url ="your_url"; 
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
// The result is here, in response string
Toast.makeText(getApplicationContext(),responseString,1000).show();
} catch(Exception e) {
}
}