我正在尝试与服务器建立连接。但它没有连接或建立连接。
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends ActionBarActivity {
TextView text;
String http_url = "http://sampleprogramz.com";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.textView2);
try {
URL url = new URL(http_url);
executeReq(url);
text.setText("HttpURLConnection Available");
}
catch(Exception e) {
text.setText("Connection Failed");
}
}
private void executeReq(URL url) throws IOException {
// TODO Auto-generated method stub
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(3000);
con.setConnectTimeout(3500);
con.setRequestMethod("GET");
con.setDoInput(true);
// Connect
con.connect();
}
}
我试图询问程序是否有连接。 我的AndroidManifest.xml文件中也有Internet权限。 你知道为什么这不起作用吗?
这里我有我的LogCat的文件,但我不确定这是不是正确的文件,因为我是新的:
08-06 16:12:15.931: I/art(29675): Background sticky concurrent mark sweep GC freed 4988(324KB) AllocSpace objects, 0(0B) LOS objects, 28% free, 798KB/1117KB, paused 6.482ms total 239.323ms
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): I got an error
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): android.os.NetworkOnMainThreadException
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at java.net.InetAddress.getAllByName(InetAddress.java:215)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.example.david.simpleserverrequest.MainActivity.executeReq(MainActivity.java:54)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.example.david.simpleserverrequest.MainActivity.onCreate(MainActivity.java:31)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.app.Activity.performCreate(Activity.java:5990)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.app.ActivityThread.access$800(ActivityThread.java:151)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.os.Handler.dispatchMessage(Handler.java:102)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.os.Looper.loop(Looper.java:135)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.app.ActivityThread.main(ActivityThread.java:5257)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at java.lang.reflect.Method.invoke(Native Method)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at java.lang.reflect.Method.invoke(Method.java:372)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
08-06 16:12:17.427: D/gralloc_goldfish(29675): Emulator without GPU emulation detected.
08-06 16:12:17.451: I/art(29675): Background partial concurrent mark sweep GC freed 882(120KB) AllocSpace objects, 0(0B) LOS objects, 52% free, 910KB/1934KB, paused 1.354ms total 284.542ms
答案 0 :(得分:0)
您正在尝试在主线程上调用方法。您需要在后台处理程序或新线程中执行此操作。
答案 1 :(得分:-1)
不是手动执行,而是有一个库可用于处理连接并处理所有连接相关问题的其余部分。尝试使用http://loopj.com/android-async-http/