Android HTTPGet错误
Android的新功能。尝试制作一个简单的应用程序,在按下按钮时执行对特定站点的httpget请求。按钮工作,祝酒器工作,但在执行httpget时我会出错。谢谢你的帮助 以下是我所拥有的:Android HTTPGet错误,android,http-get,Android,Http Get,Android的新功能。尝试制作一个简单的应用程序,在按下按钮时执行对特定站点的httpget请求。按钮工作,祝酒器工作,但在执行httpget时我会出错。谢谢你的帮助 以下是我所拥有的: package com.example.impdmxcontroller; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apac
package com.example.impdmxcontroller;
import java.io.IOException;
import org.apache.http.HttpEntity;
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 org.apache.http.util.EntityUtils;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Ch1on(View view) throws ClientProtocolException, IOException {
Toast.makeText(this, "Ch 1 On!", Toast.LENGTH_SHORT).show();
try {
HttpClient client = new DefaultHttpClient();
String getURL = "https://www.google.com";
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
//do something with the response
Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void Ch1off(View view) throws ClientProtocolException, IOException {
Toast.makeText(this, "Ch 1 Off!", Toast.LENGTH_SHORT).show();
try {
HttpClient client = new DefaultHttpClient();
String getURL = "https://www.google.com";
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
//do something with the response
Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
HTTP get位于主视图线程中。有两种处理方法。创建一个新线程来运行网络任务,或者在onCreate()方法中添加这两行
你能告诉我你遇到了什么样的错误吗?在android.os.StrictMode$AndroidLockGuardPolicy.onNetwork(StrictMode.java:1208)在java.net.InetAddress.lookupHostByName(InetAddress.java:388)在java.net.InetAddress.getAllByNameImpl(InetAddress.java:239)在java.net.InetAddress.getAllByName(InetAddress.java:214)上在org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)在org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)我怎么做?谢谢
StrictMode.ThreadPolicy ourPolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(ourPolicy);