java.lang.NullPointerException:从服务器获取json文件时lock == null

时间:2012-12-06 10:33:11

标签: android httpclient

我收到了以下错误消息。 java.lang.NullPointerException:当从服务器获取json文件时,锁= = null。我用Google搜索但无法找到任何解决方案。

我的代码:

try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            Log.i("clock", httpPost.getURI().toString());
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();           

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

        try {
            Log.i("Buffer", "1");
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            Log.i("Buffer", "2");
            StringBuilder sb = new StringBuilder();
            Log.i("Buffer", "3");
            String line = null;
            Log.i("Buffer", "4");
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            Log.i("Buffer", "5");
            json = sb.toString();
            Log.i("Buffer", "6->"+json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result-> " + e.toString());
        }

logcat的:

enter image description here

3 个答案:

答案 0 :(得分:3)

在随时使用之前,您应该始终检查instance/ java objectnot null

例如。

if( httpResponse != null){
    //todo your work here with "httpResponse "
}

if(is !=null){
   //todo your work here with "is"
}

以下是我与服务器进行通信的代码示例

// this method takes URL as input and returns JSON data 
//parent class name is "NewWebHelper.java"

public String getResult(String url) {

        Log.v(TAG, "Final Requsting URL is : :"+url);

        String line = "";
        String responseJsonData = null;

        try {
            StringBuilder sb = new StringBuilder();
            String x = "";
            URL httpurl = new URL(url);
            URLConnection  tc= httpurl.openConnection();   

            BufferedReader in = new BufferedReader(
                              new InputStreamReader(tc.getInputStream()));

            if(in !=null){
                while ((line = in.readLine()) != null) {
                sb.append(line + "\n");
                x = sb.toString();
            }
            responseJsonData = new String(x);

            }
        }
        catch (UnknownHostException uh){            
            Log.v("NewWebHelper", "Unknown host :");
            uh.printStackTrace();
        }
       catch (FileNotFoundException e) {
           Log.v("NewWebHelper", "FileNotFoundException :");
            e.printStackTrace();
         }
        catch (IOException e) {
            Log.v("NewWebHelper", "IOException :");
            e.printStackTrace();
        }
      catch (Exception e) {
          Log.v("NewWebHelper", "Exception :");
            e.printStackTrace();
        }
        return responseJsonData;
    }

如果您对此进行重新调整,请告诉我。

答案 1 :(得分:2)

例如:

        JSONObject jRoot = new JSONObject(json);
        if (!jRoot.isNull("response")) {

            JSONObject jChat = jRoot.getJSONObject("response");

            if (!jChat.isNull("type")) {
                ch.setType(jChat.getString("type"));
            }

使用函数isNull或使用条件if (is != null)

答案 2 :(得分:0)

您只需在Manifest中授予Internet访问权限。

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

为我工作......