连接到REST服务SDK25 +时出现IOexception

时间:2018-03-27 14:49:13

标签: android json http

所以我试图从REST API中读取JSON;但是,在尝试使用Scanner读取JSON时,我不断收到IOException。

目标是从API中读取所有JSON,并将其输出到Logcat。

运行调试器后,在“InputStream inputStream = conn.getInputStream();”处抛出异常。

“IsConnected”也返回false,所以我想知道这是否也是防火墙问题;但是,我认为不是这样的。

我需要使用API​​25及更高版本。

我有什么明显的错误吗?

我是学生,更喜欢一个小解释,而不仅仅是一个快速的代码修复!谢谢!

编辑: 在代码下面添加了stackTrace。

    import android.app.Activity;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

import javax.net.ssl.HttpsURLConnection;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(isConnected()) {
            Log.i("Status", "Connected");
            WebAsyncTask task = new WebAsyncTask();

            task.execute("http://services.groupkt.com/country/get/all");
        }else {
            Log.i("Status", "Not Connected");
        }

    }

    public boolean isConnected(){
        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected())
            return true;
        else
            return false;
    }

}

class WebAsyncTask extends AsyncTask<String,String,Void>{

    @Override
    protected Void doInBackground(String... strings) {
        try {
            URL url = new URL(strings[0]);
          //  Log.i("URL",url.toString());
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            InputStream inputStream = conn.getInputStream();

            Scanner scanner = new Scanner(inputStream);
            StringBuilder builder = new StringBuilder();
            while(scanner.hasNext()){
                builder.append(scanner.nextLine());
            }
            String data = builder.toString();
            Gson gson = new Gson();
            Message[] msg = gson.fromJson(data,Message[].class);
            for (int i=1;i<msg.length;i++){
              //  publishProgress(msg[i].getMessage()+"\n");
                Log.i("values", msg[i].getMessage()+"\n");
        }
        }catch(MalformedURLException e){
            e.printStackTrace();
            publishProgress("URL PROBLEM");
        }catch (IOException e){
            e.printStackTrace();
            publishProgress("Input Problem");

        }
        return null;
    }
    @Override
    protected void onProgressUpdate(String... values){
        super.onProgressUpdate();
        Log.i("values", values[0]+"\n");
    }
}
class Message{
    int status;
    String message;
    int junk;

    public int getStatus() {
        return status;
    }

    public String getMessage() {
        return message;
    }

    public int getJunk() {
        return junk;
    }
}


03-27 10:53:46.733 31032-31047/? W/System.err: java.net.UnknownHostException: Unable to resolve host "services.groupkt.com": No address associated with hostname
03-27 10:53:46.734 31032-31047/? W/System.err:     at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:125)
03-27 10:53:46.734 31032-31047/? W/System.err:     at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
03-27 10:53:46.734 31032-31047/? W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:752)
03-27 10:53:46.735 31032-31047/? W/System.err:     at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
03-27 10:53:46.735 31032-31047/? W/System.err:     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187)
03-27 10:53:46.735 31032-31047/? W/System.err:     at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156)
03-27 10:53:46.735 31032-31047/? W/System.err:     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98)
03-27 10:53:46.735 31032-31047/? W/System.err:     at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:345)
03-27 10:53:46.735 31032-31047/? W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328)
03-27 10:53:46.735 31032-31047/? W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
03-27 10:53:46.736 31032-31047/? W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
03-27 10:53:46.736 31032-31047/? W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405)
03-27 10:53:46.736 31032-31047/? W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:243)
03-27 10:53:46.737 31032-31047/? W/System.err:     at ca.on.sl.comp208.lhahncomp208a3m1.WebAsyncTask.doInBackground(MainActivity.java:59)
03-27 10:53:46.737 31032-31047/? W/System.err:     at ca.on.sl.comp208.lhahncomp208a3m1.WebAsyncTask.doInBackground(MainActivity.java:51)
03-27 10:53:46.737 31032-31047/? W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:304)
03-27 10:53:46.737 31032-31047/? W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-27 10:53:46.737 31032-31047/? W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
03-27 10:53:46.737 31032-31047/? W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
03-27 10:53:46.737 31032-31047/? W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
03-27 10:53:46.737 31032-31047/? W/System.err:     at java.lang.Thread.run(Thread.java:761)
03-27 10:53:46.738 31032-31047/? W/System.err: Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
03-27 10:53:46.738 31032-31047/? W/System.err:     at libcore.io.Posix.android_getaddrinfo(Native Method)
03-27 10:53:46.738 31032-31047/? W/System.err:     at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
03-27 10:53:46.738 31032-31047/? W/System.err:     at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:106)

1 个答案:

答案 0 :(得分:0)

我刚遇到同一问题。从堆栈跟踪中您可以得到

  

03-27 10:53:46.733 31032-31047 /? W / System.err:   java.net.UnknownHostException:无法解析主机   “ services.groupkt.com”:没有与主机名关联的地址

这似乎有一个错误的基本URL,我可以通过在您的代码中替换以下内容来解决

task.execute("http://services.groupkt.com/country/get/all");

进入

task.execute("http://groupkt.com/country/get/all");