java.lang.RuntimeException简单的Http获取代码

时间:2014-11-29 13:59:43

标签: android android-asynctask android-studio

这个简单且组织良好的代码无效。代码使用AsyncTask及其来自互联网的示例代码。Sample Code我正在使用android studio并尝试反复启动它。

package com.example.jakiro.jakki;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new Rush().execute();
}
private class Rush extends AsyncTask {
    @Override
    protected Object doInBackground(Object[] objects) {
        makePostRequest();
        return null;
    }
}
private void makePostRequest() {
    HttpClient httpClient = new DefaultHttpClient();
    // replace with your url
    HttpGet httpPost = new HttpGet("https://www.google.com");
    HttpResponse response;
    try {
        response = httpClient.execute(httpPost);
        Log.d("Response of GET request", response.toString());
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
    }

错误代码:

java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:300)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)
 Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=www.example.com
        at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
        at com.example.nova.myapplication.MainActivity.makePostRequest(MainActivity.java:66)
        at com.example.nova.myapplication.MainActivity.access$100(MainActivity.java:22)
        at com.example.nova.myapplication.MainActivity$Rush.doInBackground(MainActivity.java:36)
        at android.os.AsyncTask$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)

清单:

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.example.jakiro.jakki.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

1 个答案:

答案 0 :(得分:1)

    public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Rush().execute();
    }
    private class Rush extends AsyncTask {
        @Override
        protected Object doInBackground(Object[] objects) {
            makePostRequest();
            return null;
        }

    private void makePostRequest() {
        HttpClient httpClient = new DefaultHttpClient();
        // replace with your url
        HttpGet httpPost = new HttpGet("https://www.google.com");
        HttpResponse response;
        try {
            response = httpClient.execute(httpPost);
            Log.d("Response of GET request", response.toString());
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      }
     }
   }

你的makePostRequest()方法应该是Rust类的一部分