android 网络访问
场景:android 网络访问(高版本的出错)
android 网络访问(高版本的报错)
很普通的代码,在2.x下可以,在4.x不行。请指教。
*****************AndroidMainfest.xml*************************
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xhttpget02"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.xhttpget02.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
****************************网络访问允许啊************************
**********activity_main.xml*******************
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="btnOnClick"
android:text="Button" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button"
android:layout_marginTop="21dp"
android:text="TextView" />
</RelativeLayout>
********************就是一个按钮,一个文本框啊**********************
*************** MainActivity.java**************************
package com.example.xhttpget02;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button btn = null;
private TextView vtext = null;
private static boolean flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vtext = (TextView) findViewById(R.id.textView);
btn =(Button) findViewById(R.id.button);
flag = false;
}
public void btnOnClick(View V){
if (flag == true) {
flag = false;
vtext.setText("yes");
}else{
flag = true;
HttpURLConnection conn = null;
InputStream inputStream = null;
try{
URL url = new URL("http://services.odata.org/Northwind/Northwind.svc/Customers('ALFKI')/ContactName/$value");
conn = (HttpURLConnection) url.openConnection();
inputStream = new BufferedInputStream(conn.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String result = "";
String line = "";
while ((line=reader.readLine()) != null) {
result = result + line;
}
vtext.setText(result);
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}catch(Exception e){
Log.d("01", "have error... ");
e.printStackTrace();
}finally {
try{
inputStream.close();
conn.disconnect();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
}
**************************************************************
在2.x是没有问题的,但在4.x有问题。
真的搞不懂事什么原因啊。难道是那个http://services.odata.org/Northwind/Northwind.svc/Customers('ALFKI')/ContactName/$value不对。。。
谢谢指教。
------解决方案--------------------
访问网络代码不能放到ui线程中。
------解决方案--------------------
开个子线程 , 可好 ? Runable ?
------解决方案--------------------
4.0以上的版本要访问网络的话就要新建个线程,网络上的下载或者其他操作都要在新线程中完成。
------解决方案--------------------
在新线程外边new一个Handler类,例如 Handler h = new Handler(); 然后 h.post(Runnable r); 参数的那个Runnable用你具体的那个Runnable对象。
android 网络访问(高版本的报错)
很普通的代码,在2.x下可以,在4.x不行。请指教。
*****************AndroidMainfest.xml*************************
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xhttpget02"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.xhttpget02.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
****************************网络访问允许啊************************
**********activity_main.xml*******************
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="btnOnClick"
android:text="Button" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button"
android:layout_marginTop="21dp"
android:text="TextView" />
</RelativeLayout>
********************就是一个按钮,一个文本框啊**********************
*************** MainActivity.java**************************
package com.example.xhttpget02;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button btn = null;
private TextView vtext = null;
private static boolean flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vtext = (TextView) findViewById(R.id.textView);
btn =(Button) findViewById(R.id.button);
flag = false;
}
public void btnOnClick(View V){
if (flag == true) {
flag = false;
vtext.setText("yes");
}else{
flag = true;
HttpURLConnection conn = null;
InputStream inputStream = null;
try{
URL url = new URL("http://services.odata.org/Northwind/Northwind.svc/Customers('ALFKI')/ContactName/$value");
conn = (HttpURLConnection) url.openConnection();
inputStream = new BufferedInputStream(conn.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String result = "";
String line = "";
while ((line=reader.readLine()) != null) {
result = result + line;
}
vtext.setText(result);
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}catch(Exception e){
Log.d("01", "have error... ");
e.printStackTrace();
}finally {
try{
inputStream.close();
conn.disconnect();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
}
**************************************************************
在2.x是没有问题的,但在4.x有问题。
真的搞不懂事什么原因啊。难道是那个http://services.odata.org/Northwind/Northwind.svc/Customers('ALFKI')/ContactName/$value不对。。。
谢谢指教。
------解决方案--------------------
访问网络代码不能放到ui线程中。
------解决方案--------------------
开个子线程 , 可好 ? Runable ?
------解决方案--------------------
4.0以上的版本要访问网络的话就要新建个线程,网络上的下载或者其他操作都要在新线程中完成。
------解决方案--------------------
在新线程外边new一个Handler类,例如 Handler h = new Handler(); 然后 h.post(Runnable r); 参数的那个Runnable用你具体的那个Runnable对象。