如何检索网站中的值?
这是我的java代码,
public void String[] receive()
{
String url="http://shrib.com/content123";
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s[];
s=buffer.readLine().split(" ");
return s;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
运行此代码时,应用程序已停止
AndroidManifest.xml中的
<uses-permission android:name="android.permission.INTERNET" />
也被添加..
然而代码失败了。
例如)考虑www.shrib.com/content123
中的值 10 20 30 40
每个都用空格隔开 所以使用split(&#34;&#34;)函数来分隔值......
我的堆栈跟踪异常
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1156)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:587)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:511)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:489)
at vehicle_monitor25.com.example.Main1.receive(Main1.java:182)
at vehicle_monitor25.com.example.Main1.onCreate(Main1.java:58)
at android.app.Activity.performCreate(Activity.java:5343)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Unable to start activity ComponentInfo{vehicle_monitor25.com.example/vehicle_monitor25.com.example.Main1}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
at android.os.Handler.dispatchMessage(Handler.java:110)
... 11 more
执行asyntask后
public class Main1 extends Activity{
TextView tv1,tv2,tv3,tv4;
Button bt1,bt2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
int t1,t2,t3,t4;
String s1,s2,s3,s4;
t1=10;
tv1=(TextView) findViewById(R.id.tv1);
tv1.setText("10");
new NetworkOperation.execute("");
}
public class NetworkOperation extends AsyncTask<Void, Void, Void>
{
protected String[] doInBackground(String... params) {
//Do your network operation here
String url = "http://www.shrib.com/sridhar123";
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s[];
s=buffer.readLine().split(" ");
return s;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String[] result) {
tv1.setText(result[0]);
}
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
}
}
无法解析NetworkOperation以输入错误
答案 0 :(得分:-1)
使用asyncTask线程实现网络操作。
class NetworkOperation extends AsyncTask<Void, Void, Void>
{
@Override
protected String doInBackground(String... params) {
//Do your network operation here
return null;
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute() {
}
}
要调用此类,请使用
new NetworkOperation.execute();