Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单rest api get调用无法运行android_Android_Rest - Fatal编程技术网

简单rest api get调用无法运行android

简单rest api get调用无法运行android,android,rest,Android,Rest,下面的代码不起作用 String url = "http://my/url/username/pswd"; String result = ""; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv; tv = (TextView)

下面的代码不起作用

String url = "http://my/url/username/pswd";
String result = "";

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView tv;
    tv = (TextView) findViewById(R.id.text1);

    HttpClient httpclient = new DefaultHttpClient();  
    HttpGet request = new HttpGet(url);  
    request.addHeader("apikey", "DeveloperWy7ayxR");
    request.addHeader("Content-Type","application/json");

    ResponseHandler handler = new BasicResponseHandler();  
    try {  
        result = httpclient.execute(request, handler);  
    } catch (ClientProtocolException e) {  
        e.printStackTrace();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  

    httpclient.getConnectionManager().shutdown();  
    Log.i("myLog", result); 

    tv.setText(result);
}

我用高级rest客户端测试了url和头文件,效果很好。我错过什么了吗

您应该在后台进行联网操作。例如,在

有关更多信息,请参阅

并在android manifest.xml文件中添加网络权限:

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

现在我正在尝试asynktask。如果它也不起作用,我会更新我的Question@SGG好的,让我们试着回答。谢谢,我看到了asynctask的例子。但我不知道他们为什么要用。现在我明白了。@SGG很乐意帮忙,祝你愉快。