0

So I Googled and found examples similar to what i need. I've tweaked and adjusted and solved most of my problems but have become confounded by this problem which i cannot seem to pass. Some Backround: So I'm building an App that query's a MySQL data base through PHP producing a JSON object. I can see "[{"question":"Why is the sky blue"}]" in my web browser on the SDK so I know I have access to the object. Permissions are set properly in my manifest as well. The problem comes when i try to parse the object to a string. I get "Error converting result java.lang.NullPointerException" from my log cat.

Now I've tried

//convert response to string
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String json = reader.readLine();
        JSONTokener tokener = new JSONTokener(json);
        JSONArray finalResult = new JSONArray(tokener);

          }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
          }

I've also tried

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

          }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
          }

I guess I'll post my http (even in its own thread best practices yay! - but then i use global variables (facepalm))

    new Thread() {
    public void run() {
          try{
               HttpClient httpclient = new DefaultHttpClient();

                          HttpPost httppost = new HttpPost("http://192.168.1.55/qgrap.php");

                          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                          HttpResponse response = httpclient.execute(httppost);

               }catch(Exception e){
                   Log.e("log_tag", "Error in http connection"+e.toString());
              } 
    }
    }.start();

In the HTTP for the second code set you need to do some get content stuff either way I'd like to focus on the first code set. Here response is a global variable. which gets manipulated as it gets passed into BufferReader arguments. I'f I'm not mistaken at the point of String json = reader.readLine(); I should have access to the data as a string. Either way log says no go and i have no idea why. Also Can't I just create a string on the php side instead of using JSON?

BOUNTY: A Thousand Internets

3 Answers 3

1

I recommend GSON http://code.google.com/p/google-gson/

2
  • Awesome thank you. I'm pretty sure that's what I'm looking for. I've got alot of homework today so it will be a moment before I test it. If it does work however I may not mark this as answer. Lets say my app takes off (it's looking good) That will put more work server side and dependent on the spec of my server and how ever many people pick this thing up (its free) that could be alot of overhead. so I'd like to leave this open for just a little while longer. But again, thank you. Commented Feb 1, 2012 at 18:06
  • So I'm dumb Gson is not server side I only briefly reviewed the documentation. So give me a moment because I'm working on my code. I think you got it. Commented Feb 1, 2012 at 23:46
0

Alright so I'm scuttling this because I don't want to use gson i want my code to work. I have one error that i don't understand Error converting result java.lang.NullPointerException boom that's it.

     try{
    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
    String json = reader.readLine();
    JSONTokener tokener = new JSONTokener(json);
    JSONArray finalResult = new JSONArray(tokener);

      }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
      }

Somewhere in this block of code is the problem. I'm not seeing it. Either way im going to html5

0

You should bundle up your input in some sort of loop rather than assuming it is all on the first line:

BufferedReader userInfoReader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder builder = new StringBuilder();
while ((line = userInfoReader.readLine()) != null) {
    builder.append(line).append("\n");
}
JSONTokener tokener = new JSONTokener(builder.toString());
JSONArray finalResult = new JSONArray(tokener);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.