-1
Android新手。嘗試做一個簡單的應用程序來執行一個httpget請求時,按下按鈕的某個網站。按鈕的工作和敬酒工作,但我得到錯誤時,httpget執行。感謝您的幫助....Android HTTPGet錯誤
以下是我有:
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;
}
}
你能告訴你得到了什麼樣的錯誤嗎? – Alamri
在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1208) \t在java.net.InetAddress.lookupHostByName(InetAddress.java:388) \t在java.net.InetAddress.getAllByNameImpl(InetAddress.java: 239) \t在java.net.InetAddress.getAllByName(InetAddress.java:214) \t在org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) \t在org.apache.http .impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164 – user2535660
我該怎麼做?謝謝BTW – user2535660