0

in the following code, i am trying to perform a GET operation on a webservice that i have coded and hosted on localhost. The method OpenHttpConnection is working just fine because i have put toasts in between to check if there was sth wrong in there. the app crashes when i try to convert the input stream into a string using the bufferreader. Please have a look and see if you can spot the error.

Thanks :)

public class ServicetestActivity extends Activity {



public static String iStream_to_String(InputStream is1)
{
     BufferedReader rd = new BufferedReader(new InputStreamReader(is1));
     String line;
     StringBuilder sb =  new StringBuilder();
     try {
        while ((line = rd.readLine()) != null) {
                sb.append(line);
         }
         rd.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     String contentOfMyInputStream = sb.toString();
     return contentOfMyInputStream;
}






/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    InputStream is=null;
    try {
         is=OpenHttpConnection("http://localhost/webservice.php?device=ayaz");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String line= iStream_to_String(is);
    Toast.makeText(this, line, Toast.LENGTH_LONG).show(); 
}


    private InputStream OpenHttpConnection(String link) throws IOException {
        // TODO Auto-generated method stub
        InputStream inputStream = null;
        int response = -1;
        Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
        URL url = new URL(link);
        URLConnection connection = url.openConnection();
        Toast.makeText(this, "2", Toast.LENGTH_SHORT).show();
        if (!(connection instanceof HttpURLConnection))
          throw new IOException("Not a HTTP connection");
        try {
            HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
            Toast.makeText(this, "3", Toast.LENGTH_SHORT).show();
            httpURLConnection.setAllowUserInteraction(false);
            Toast.makeText(this, "4", Toast.LENGTH_SHORT).show();
            httpURLConnection.setInstanceFollowRedirects(true);
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.connect();
            response = httpURLConnection.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) {
                inputStream = httpURLConnection.getInputStream();
            }

        } catch (Exception e) {
            // TODO: handle exception
            throw new IOException("Error connecting");
        }
        return inputStream;}

// see http://androidsnippets.com/executing-a-http-get-request-with-httpclient

}

Also the log cat is as follows:

10-14 20:54:13.660: E/AndroidRuntime(1348): FATAL EXCEPTION: main
10-14 20:54:13.660: E/AndroidRuntime(1348): java.lang.RuntimeException: Unable to start activity ComponentInfo{web.service/web.service.ServicetestActivity}: java.lang.NullPointerException
10-14 20:54:13.660: E/AndroidRuntime(1348):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-14 20:54:13.660: E/AndroidRuntime(1348):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-14 20:54:13.660: E/AndroidRuntime(1348):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-14 20:54:13.660: E/AndroidRuntime(1348):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-14 20:54:13.660: E/AndroidRuntime(1348):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 20:54:13.660: E/AndroidRuntime(1348):     at android.os.Looper.loop(Looper.java:130)
10-14 20:54:13.660: E/AndroidRuntime(1348):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-14 20:54:13.660: E/AndroidRuntime(1348):     at java.lang.reflect.Method.invokeNative(Native Method)
10-14 20:54:13.660: E/AndroidRuntime(1348):     at java.lang.reflect.Method.invoke(Method.java:507)
10-14 20:54:13.660: E/AndroidRuntime(1348):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-14 20:54:13.660: E/AndroidRuntime(1348):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-14 20:54:13.660: E/AndroidRuntime(1348):     at dalvik.system.NativeStart.main(Native Method)
10-14 20:54:13.660: E/AndroidRuntime(1348): Caused by: java.lang.NullPointerException
10-14 20:54:13.660: E/AndroidRuntime(1348):     at java.io.Reader.<init>(Reader.java:65)
10-14 20:54:13.660: E/AndroidRuntime(1348):     at java.io.InputStreamReader.<init>(InputStreamReader.java:122)
10-14 20:54:13.660: E/AndroidRuntime(1348):     at java.io.InputStreamReader.<init>(InputStreamReader.java:59)
10-14 20:54:13.660: E/AndroidRuntime(1348):     at web.service.ServicetestActivity.iStream_to_String(ServicetestActivity.java:31)
10-14 20:54:13.660: E/AndroidRuntime(1348):     at web.service.ServicetestActivity.onCreate(ServicetestActivity.java:65)
10-14 20:54:13.660: E/AndroidRuntime(1348):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-14 20:54:13.660: E/AndroidRuntime(1348):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-14 20:54:13.660: E/AndroidRuntime(1348):     ... 11 more
Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53
Salik
  • 508
  • 6
  • 17
  • 35

3 Answers3

0

This may help you.

   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();

            return result;
         }catch (Exception e) {}
Rajendra
  • 1,700
  • 14
  • 17
  • it does not let the app crash due to try catch, but it still does not convert the is into string and hence not returning the desired result. it goes into catch – Salik Oct 14 '12 at 16:48
  • what exception you are getting in catch? – Rajendra Oct 14 '12 at 16:50
0

The log show information as follows:

FATAL EXCEPTION: main 10-14 20:54:13.660:E/AndroidRuntime(1348):java.lang.RuntimeException: 
Unable to start activity ComponentInfo{web.service/web.service.ServicetestActivity}:
java.lang.NullPointerException 10-14 20:54:13.660: E/AndroidRuntime(1348):

and then:

 Caused by: java.lang.NullPointerException 10-14 20:54:13.660: 
 E/AndroidRuntime(1348): at java.io.Reader.(Reader.java:65) 10-14 20:54:13.660:
 E/AndroidRuntime(1348): at java.io.InputStreamReader.(InputStreamReader.java:122) 
 10-14 20:54:13.660: E/AndroidRuntime(1348): at java.io.InputStreamReader. 
 (InputStreamReader.java:59) 10-14 20:54:13.660: E/AndroidRuntime(1348): 
 at web.service.ServicetestActivity.iStream_to_String(ServicetestActivity.java:31) 
 10-14 20:54:13.660: E/AndroidRuntime(1348): 
 at web.service.ServicetestActivity.onCreate(ServicetestActivity.java:65) 

means your app is crashing at line no. 31 in iStream_to_String. So, check at this line no. what is going wrong.

For checking condition, if something goes wrong, prefer to use Log other than Toast, as Toast is not for checking errors.

Nitish
  • 3,097
  • 13
  • 45
  • 80
  • yes i dont use toast as error checker, i was just using it to track the progression, i did check the line 31 and it redirects to the method where i am trying to convert the stream into string and i know the problem lies there but i cant seem to figure out what exactly is the problem because code seems fine – Salik Oct 14 '12 at 16:49
  • 1
    Problem is at this line: BufferedReader rd = new BufferedReader(new InputStreamReader(is1)); Means you don't have anything in the InputStream that you have passed to the InputStreamReader. So, check once, whether you are getting anything from your localhost. – Nitish Oct 14 '12 at 17:01
  • BTW, which is line no. 31, in your case. – Nitish Oct 14 '12 at 17:14
0

I got my problem solved. The problem was not in buffer reader but my connection was being refused. The connection was being refused because the link that I passed referred to localhost(my machine). But in actuality Android takes localhost as the emulator itself, and since there was no server hosted it kept on refusing the connection. The solution to this is that I used my machine's ip instead of localhost so it would be http://x.x.x.x/......

Bart
  • 19,692
  • 7
  • 68
  • 77
Salik
  • 508
  • 6
  • 17
  • 35