无法从Url获​​得回复

时间:2014-08-07 21:55:27

标签: android json android-asynctask

我尝试使用webapi从mvc 4应用程序获取json文件,但是在asynctask doinbackground方法中出现错误

erro在json解析器中

Buffer Error", "Error converting result null pointer exception

并且还有其他错误,但是因为服务器没有响应而导致错误

我试图调试我的代码,当我到达此代码的行时,应用程序崩溃

public JSONObject getJSONFromUrl(String Url) throws JSONException
    {

        try {
            Log.d("tony","6"+Url);
            DefaultHttpClient httpClient = new DefaultHttpClient(); // to connect to http
            Log.d("tony","7"+Url);
            HttpGet httpGet=new HttpGet(Url);
            Log.d("tony","8"+httpGet.getMethod());
            HttpResponse httpResponse = httpClient.execute(httpGet);

我正在使用iis 8.0 json解析器

 package com.haddad.tony;



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.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

    static InputStream is;
    static JSONObject jObj;
    static String json = "";

    public JSONParser()
    {

    }

    public JSONObject getJSONFromUrl(String Url) throws JSONException
    {

        try {
            DefaultHttpClient httpClient = new DefaultHttpClient(); // to connect to http
            HttpGet httpGet=new HttpGet(Url);
            HttpResponse httpResponse = httpClient.execute(httpGet); //get response from the url

            HttpEntity httpEntity = httpResponse.getEntity(); // extract data from response

            is = httpEntity.getContent(); // putting the extracted data in inputstream

        } catch (UnsupportedEncodingException e) {
            // TODO: handle exception
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO: handle exception
            e.printStackTrace();
        } catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);

            StringBuilder sb = new StringBuilder();
            String Line = null;

            //Adding json!
            while ((Line = reader.readLine()) != null)
            {
                sb.append(Line + "\n");
            }
            is.close();
            json = sb.toString();

        } catch (Exception e) {
            // TODO: handle exception

            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        try {
            Log.d("ss",json);
            jObj = new JSONObject(json); // parse the string to json Object
        } catch (JSONException e) {
            // TODO: handle exception
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        //this part is secondary. used when JSON has hidden string within the http to clean we use this return with throwing exceptions
        return new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1)); 
    }

    public JSONObject makeHttpRequest(String Url, String Method, List<NameValuePair> params)
    {
        try {

            if (Method == "POST")
            {
                // request method is post
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(Url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }
            else if(Method == "GET")
            {

                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                Url += "?" + paramString;

                Log.d("URL", Url);

                 HttpGet httpGet = new HttpGet(Url);
                 HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();

                is = httpEntity.getContent();
            }   
        } catch (UnsupportedEncodingException e) {
            // TODO: handle exception
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO: handle exception
            e.printStackTrace();
        } catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String Line = null;

            while ((Line = reader.readLine()) != null)
            {
                sb.append(Line + "\n");
            }

            is.close();
            json = sb.toString();
            Log.d("myjson",json);
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        try{
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            // TODO: handle exception
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        return jObj;
    }

}

任何帮助?

1 个答案:

答案 0 :(得分:0)

我通过将我的应用程序绑定到我的公共IP地址来修复我的问题

IIS applicationhost.config

中的D:\Documents\IISExpress\config

使用此

  <site name="StopeAndShop" id="15">
                <application path="/" applicationPool="Clr4IntegratedAppPool">
                    <virtualDirectory path="/" physicalPath="D:\Desktop\StopeAndShopTEST\StopeAndShopTEST\StopeAndShop" />
                </application>
                <bindings>
                    <binding protocol="http" bindingInformation="*:2100:localhost" />
                    <binding protocol="http" bindingInformation="*:2100:myip address" />
                </bindings>
            </site>

并允许使用此命令进行连接

netsh http add urlacl url=http://myip address:2100/ user=everyone