-4

I'm sorry to ask a stupid question, but I need help

I refer to this answer, trying to use "GET" requests, and download page But when I press the button, the application crashes


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et = (EditText)findViewById(R.id.editText);
    et2 = (EditText)findViewById(R.id.editText2);
}

public void add(View view){ //button
        et2.setText(testget());
}

    public String testget(){
    HttpResponse response = null;
    try {
        HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    request.setURI(new URI("http://61.218.228.98:8008/Verify.aspx?EleSignVerift=421E313B"));
    response = client.execute(request);
} catch (URISyntaxException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
return response.toString();

}

I want to use the same way, a request to set up my site and download the results

I do not know where the wrong

Or I used the wrong way?

edit ---Error Messages

06-09 14:19:42.112    5470-5470/com.example.sw.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: Could not execute method of the activity



at android.view.View$1.onClick(View.java:3598)

at android.view.View.performClick(View.java:4091)

at android.view.View$PerformClick.run(View.java:17072)

at android.os.Handler.handleCallback(Handler.java:615)

at android.os.Handler.dispatchMessage(Handler.java:92)

at android.os.Looper.loop(Looper.java:153)

at android.app.ActivityThread.main(ActivityThread.java:5079)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:511)



at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:826)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:589)

at dalvik.system.NativeStart.main(Native Method)

     Caused by: java.lang.reflect.InvocationTargetException

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:511)

at android.view.View$1.onClick(View.java:3593)

at android.view.View.performClick(View.java:4091)

at android.view.View$PerformClick.run(View.java:17072)

at android.os.Handler.handleCallback(Handler.java:615)

at android.os.Handler.dispatchMessage(Handler.java:92)

at android.os.Looper.loop(Looper.java:153)

at android.app.ActivityThread.main(ActivityThread.java:5079)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:511)

at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:826)


at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:589)

at dalvik.system.NativeStart.main(Native Method)

     Caused by: java.lang.NullPointerException

at com.example.sw.myapplication.MainActivity.testget(MainActivity.java:64)

at com.example.sw.myapplication.MainActivity.add(MainActivity.java:45)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:511)

at android.view.View$1.onClick(View.java:3593)

at android.view.View.performClick(View.java:4091)

at android.view.View$PerformClick.run(View.java:17072)

at android.os.Handler.handleCallback(Handler.java:615)

at android.os.Handler.dispatchMessage(Handler.java:92)

at android.os.Looper.loop(Looper.java:153)

at android.app.ActivityThread.main(ActivityThread.java:5079)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:511)

at om.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:826)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:589)

at dalvik.system.NativeStart.main(Native Method)

---AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <uses-permission android:name="android.permission.INTERNET"/>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".GenerateQRCodeActivity"
        android:label="@string/title_activity_generate_qrcode" >
    </activity>
</application>

Community
  • 1
  • 1
AM031447
  • 475
  • 1
  • 8
  • 21

2 Answers2

1

Your response variable is null in case of any exceptions. Do you perform your internet connection on the background thread? Also, response.toString() won't return you body of response. Read more about how to get body from http response.

Orest Savchak
  • 4,529
  • 1
  • 18
  • 27
1
Caused by: java.lang.NullPointerException
at com.example.sw.myapplication.MainActivity.testget(MainActivity.java:64)
at com.example.sw.myapplication.MainActivity.add(MainActivity.java:45)

Looks like you are getting null as response and response.toString() is throwing NPE. So you should firstly make http call asynchronously ( in AsyncTask).

See AsyncTask here and here. Another SO post.

Community
  • 1
  • 1
Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72