发送" GET" http请求失败

时间:2015-06-09 05:29:18

标签: java android

我很抱歉提出一个愚蠢的问题,但我需要帮助

我指的是answer,尝试使用" GET"请求和下载页面 但是当我按下按钮时,应用程序崩溃

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

}

我想以同样的方式使用,请求设置我的网站并下载结果

我不知道哪里错了

或者我用错了方法?

修改的 ---错误信息

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>

2 个答案:

答案 0 :(得分:1)

如果有任何异常,您的response变量为空。 你在后台线程上进行互联网连接吗? 此外,response.toString()不会让您回复身体。阅读更多关于如何从http响应中获取正文的信息。

答案 1 :(得分: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)

看起来你的回复为空,response.toString()正在投掷NPE。 所以你应该首先异步进行http调用(在AsyncTask中)。

请参阅AsyncTask herehere。另一个SO post