1

I have written android class which will call RESTful web service. If request is successful, response will be JSON object. I write android class like this:

public class android extends Activity {

    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView txt = (TextView) findViewById(R.id.textView1);
    txt.setText(getInputStreamFromUrl("http://localhost:8080/kyaw"));
    }

    public static String getInputStreamFromUrl(String url) {
          InputStream content = null;
          try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(new HttpGet(url));
            content = response.getEntity().getContent();
          } catch (Exception e) {
            Log.e("[GET REQUEST]", "Network exception");
          }
            String result=convert(content);
            return result;
        }

    private static String convert(InputStream in)
    {
        BufferedReader reader=new BufferedReader(new InputStreamReader(in));
        StringBuilder sb=new StringBuilder();
        String line=null;
        try{
            while((line=reader.readLine())!=null){
                sb.append(line+"\n");
            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }finally{
            try{
                in.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

}

I have a problem after i run.I will get exception coz i return as a string.But response is JSON .How should I convert JSON to String or other way and then how to show result in android screen?

thanksenter image description here

sudo
  • 1,525
  • 7
  • 34
  • 59
  • what exception? FC ? JSON response from the server is a String. – Selvin May 19 '11 at 07:47
  • hi selvin, when i run the class andriod virtual device alert me there is exception then close but don't show what exception. I debug it , i found exception at HttpResponse response = httpclient.execute(new HttpGet(url)); . I don't know why and i am new to it – sudo May 19 '11 at 07:57
  • First enable Logcat http://stackoverflow.com/questions/3280051/how-to-enable-logcat-in-eclipse . second provide logs from logcat window – Selvin May 19 '11 at 07:59
  • hi selvin , can u see exception ? it is not very clear – sudo May 19 '11 at 08:10
  • no i cant ... you cut log in the most importent part ... do not copy logcat as image copy rows from logcat and provide em as text here – Selvin May 19 '11 at 08:13
  • The first thing I'd probably do would be to identify exactly what is causing the NullPointerException. If you're sure the exception originates from the call to httpclient.execute, are you also sure the parameter to the HttpGet constructor is not null? – Programmer Bruce May 19 '11 at 08:20
  • 05-19 07:35:56.523: ERROR/AndroidRuntime(498): FATAL EXCEPTION: main 05-19 07:35:56.523: ERROR/AndroidRuntime(498): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.android}: java.lang.NullPointerException 05-19 07:35:56.523: ERROR/AndroidRuntime(498): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) – sudo May 19 '11 at 08:22

2 Answers2

1

I just do this:

HttpClient client = new DefaultHttpClient();
HttpPost poster = new HttpPost("http://www.example.com/json.php");
poster.addHeader("Content-Type", "text/json");
poster.setEntity(new StringEntity(data.toString()));
//data being a json object created and filled earlier
HttpResponse response = client.execute(poster);                                 
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
    DataInputStream input = new DataInputStream(response.getEntity().getContent());
    JSONObject json = new JSONObject(input.readLine());
    json.toString(2);
}
Ray Britton
  • 8,257
  • 3
  • 23
  • 32
1

well "http://localhost:8080/kyaw" is a problem ... by this you poining to emulator not emulator's host ... and you getting network error

try "http://ip.of.your.host:8080/kyaw"

EDIT:

        content = response.getEntity().getContent();// here comes an error
      } catch (Exception e) {
        Log.e("[GET REQUEST]", "Network exception");//we catch it here
      }
      //here you got content == null
      //so you getting null point exception 
Selvin
  • 6,598
  • 3
  • 37
  • 43