Новое в 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