-2
我曾嘗試過很多方法在android中創建http請求。無論我做什麼,Logcat都會給我錯誤。我需要一些幫助。我加Android Http請求錯誤
uses-permission android:name="android.permission.INTERNET">
彰顯文件 我已經檢查了我的連接,它說,它是連接...
代碼:
package com.example.gpschat;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
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 android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
String[] messages;
LinearLayout.LayoutParams lp;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String m = "";
try {
m = excutePost();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("gilad", m);
messages = m.split(",");
LinearLayout ll = (LinearLayout) findViewById(R.id.chat);
for (String message : messages)
{
lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tv = new TextView(this);
tv.setId(1);
tv.setTextSize(15);
tv.setText(message);
tv.setLayoutParams(lp);
(ll).addView(tv);
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static String excutePost() throws IOException
{
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet("http://laurawharton.com/gps/getM.php"));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
String responseString = out.toString();
//..more logic
return responseString;
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
}
}
錯誤:
09-02 11:43:10.873: W/dalvikvm(1234): threadid=1: thread exiting with uncaught exception (group=0xb3ac8ba8)
09-02 11:43:10.893: E/AndroidRuntime(1234): FATAL EXCEPTION: main
09-02 11:43:10.893: E/AndroidRuntime(1234): Process: com.example.gpschat, PID: 1234
09-02 11:43:10.893: E/AndroidRuntime(1234): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gpschat/com.example.gpschat.MainActivity}: android.os.NetworkOnMainThreadException
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.os.Handler.dispatchMessage(Handler.java:102)
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.os.Looper.loop(Looper.java:136)
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-02 11:43:10.893: E/AndroidRuntime(1234): at java.lang.reflect.Method.invokeNative(Native Method)
09-02 11:43:10.893: E/AndroidRuntime(1234): at java.lang.reflect.Method.invoke(Method.java:515)
09-02 11:43:10.893: E/AndroidRuntime(1234): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-02 11:43:10.893: E/AndroidRuntime(1234): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-02 11:43:10.893: E/AndroidRuntime(1234): at dalvik.system.NativeStart.main(Native Method)
09-02 11:43:10.893: E/AndroidRuntime(1234): Caused by: android.os.NetworkOnMainThreadException
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
09-02 11:43:10.893: E/AndroidRuntime(1234): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-02 11:43:10.893: E/AndroidRuntime(1234): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-02 11:43:10.893: E/AndroidRuntime(1234): at java.net.InetAddress.getAllByName(InetAddress.java:214)
09-02 11:43:10.893: E/AndroidRuntime(1234): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
09-02 11:43:10.893: E/AndroidRuntime(1234): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-02 11:43:10.893: E/AndroidRuntime(1234): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-02 11:43:10.893: E/AndroidRuntime(1234): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-02 11:43:10.893: E/AndroidRuntime(1234): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-02 11:43:10.893: E/AndroidRuntime(1234): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-02 11:43:10.893: E/AndroidRuntime(1234): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-02 11:43:10.893: E/AndroidRuntime(1234): at com.example.gpschat.MainActivity.excutePost(MainActivity.java:89)
09-02 11:43:10.893: E/AndroidRuntime(1234): at com.example.gpschat.MainActivity.onCreate(MainActivity.java:44)
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.app.Activity.performCreate(Activity.java:5231)
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-02 11:43:10.893: E/AndroidRuntime(1234): ... 11 more
09-02 11:43:16.833: I/Process(1234): Sending signal. PID: 1234 SIG: 9
您是否首先在Google中搜索此問題? 2011年堆棧溢出:http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – DaniEll 2014-09-02 16:00:01