用在有root权限的手机上可以正常使用,但是没有root的就使用不了
root的机器是android 2.3 的 出现错误的是android4.0的
消息提示错误出再:
DataOutputStream conout = new DataOutputStream(con.getOutputStream());
06-28 16:48:45.340: W/dalvikvm(5957): threadid=1: thread exiting with uncaught exception (group=0x40a4d1f8)
06-28 16:48:45.340: E/AndroidRuntime(5957): FATAL EXCEPTION: main
06-28 16:48:45.340: E/AndroidRuntime(5957): android.os.NetworkOnMainThreadException
06-28 16:48:45.340: E/AndroidRuntime(5957): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.io.IoBridge.recvfrom(IoBridge.java:503)
06-28 16:48:45.340: E/AndroidRuntime(5957): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
06-28 16:48:45.340: E/AndroidRuntime(5957): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
06-28 16:48:45.340: E/AndroidRuntime(5957): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
06-28 16:48:45.340: E/AndroidRuntime(5957): at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:168)
06-28 16:48:45.340: E/AndroidRuntime(5957): at java.io.BufferedInputStream.read(BufferedInputStream.java:227)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.net.http.HttpConnection.isStale(HttpConnection.java:256)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:71)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
06-28 16:48:45.340: E/AndroidRuntime(5957): at com.cy.nearshop.utils.HttpUtils.postHttpClientString(HttpUtils.java:47)
06-28 16:48:45.340: E/AndroidRuntime(5957): at com.cy.nearshop.activity.Comment_count$3.run(Comment_count.java:202)
06-28 16:48:45.340: E/AndroidRuntime(5957): at android.os.Handler.handleCallback(Handler.java:605)
06-28 16:48:45.340: E/AndroidRuntime(5957): at android.os.Handler.dispatchMessage(Handler.java:92)
06-28 16:48:45.340: E/AndroidRuntime(5957): at android.os.Looper.loop(Looper.java:137)
06-28 16:48:45.340: E/AndroidRuntime(5957): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-28 16:48:45.340: E/AndroidRuntime(5957): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 16:48:45.340: E/AndroidRuntime(5957): at java.lang.reflect.Method.invoke(Method.java:511)
06-28 16:48:45.340: E/AndroidRuntime(5957): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-28 16:48:45.340: E/AndroidRuntime(5957): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-28 16:48:45.340: E/AndroidRuntime(5957): at dalvik.system.NativeStart.main(Native Method)
root的机器是android 2.3 的 出现错误的是android4.0的
消息提示错误出再:
DataOutputStream conout = new DataOutputStream(con.getOutputStream());
06-28 16:48:45.340: W/dalvikvm(5957): threadid=1: thread exiting with uncaught exception (group=0x40a4d1f8)
06-28 16:48:45.340: E/AndroidRuntime(5957): FATAL EXCEPTION: main
06-28 16:48:45.340: E/AndroidRuntime(5957): android.os.NetworkOnMainThreadException
06-28 16:48:45.340: E/AndroidRuntime(5957): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.io.IoBridge.recvfrom(IoBridge.java:503)
06-28 16:48:45.340: E/AndroidRuntime(5957): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
06-28 16:48:45.340: E/AndroidRuntime(5957): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
06-28 16:48:45.340: E/AndroidRuntime(5957): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
06-28 16:48:45.340: E/AndroidRuntime(5957): at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:168)
06-28 16:48:45.340: E/AndroidRuntime(5957): at java.io.BufferedInputStream.read(BufferedInputStream.java:227)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.net.http.HttpConnection.isStale(HttpConnection.java:256)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:71)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
06-28 16:48:45.340: E/AndroidRuntime(5957): at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
06-28 16:48:45.340: E/AndroidRuntime(5957): at com.cy.nearshop.utils.HttpUtils.postHttpClientString(HttpUtils.java:47)
06-28 16:48:45.340: E/AndroidRuntime(5957): at com.cy.nearshop.activity.Comment_count$3.run(Comment_count.java:202)
06-28 16:48:45.340: E/AndroidRuntime(5957): at android.os.Handler.handleCallback(Handler.java:605)
06-28 16:48:45.340: E/AndroidRuntime(5957): at android.os.Handler.dispatchMessage(Handler.java:92)
06-28 16:48:45.340: E/AndroidRuntime(5957): at android.os.Looper.loop(Looper.java:137)
06-28 16:48:45.340: E/AndroidRuntime(5957): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-28 16:48:45.340: E/AndroidRuntime(5957): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 16:48:45.340: E/AndroidRuntime(5957): at java.lang.reflect.Method.invoke(Method.java:511)
06-28 16:48:45.340: E/AndroidRuntime(5957): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-28 16:48:45.340: E/AndroidRuntime(5957): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-28 16:48:45.340: E/AndroidRuntime(5957): at dalvik.system.NativeStart.main(Native Method)
解决方案 »
- 【讨论】通过bindService启动的service,在unbindService后service也结束了
- android windows sdk现在只能安装了吗?
- 关于Instrumentation 测试如何测试OnFling()方法
- Android如何实现Wifi或者3G打开调用其他应用程序
- 在eclipse里面安装apk时,出现。。
- android 摇动事件判断
- 【反汇编软件分享】写了个Android系统下的x86反汇编器,欢迎捧场
- 真机2G网络json解析天气
- 哪位大神知道PhoneGap使用BarcodeScanner插件的具体步骤,最好是有ZXing库的那种
- 求解viewpager中的fragment切换,怎么返回数据到activity
- android 瀑布流Bitmap OOM
- ftp问题
import java.util.Map;import org.json.JSONException;
import org.json.JSONObject;import com.cy.nearshop.utils.HttpUtils;
import com.cy.nearshop.utils.InitConfig;
import com.cy.nearshop.utils.JsonUtils;
import com.cy.nearshop.vo.Comment;
import com.cy.nearshop.vo.Countent;import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;public class Comment_count extends Activity {
LinearLayout comment_countent = null;
Countent ctt = null;
ArrayList<Comment> listres = null;
EditText cvc_et = null;
String etcun = null;
String userstr = null;
/**
* 10条留言
*/
int number = 10;
HttpUtils hu=null;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.comment_view_comment);
TextView cmm_title = (TextView) findViewById(R.id.cmm2_title);
TextView cmm_content = (TextView) findViewById(R.id.cmm2_content);
TextView cmm_time = (TextView) findViewById(R.id.cmm2_time);
ImageView cmm_img = (ImageView) findViewById(R.id.cmm2_img);
ImageButton mm_back_btn = (ImageButton) findViewById(R.id.mm_back_btn);
Button cvc_butt = (Button) findViewById(R.id.cvc_butt);
cvc_et = (EditText) findViewById(R.id.cvc_et);
hu=new HttpUtils();
cvc_butt.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {
// TODO Auto-generated method stub
etcun = cvc_et.getText().toString();
SharedPreferences sp = Comment_count.this.getSharedPreferences(
InitConfig.user_init, 0);
userstr = sp.getString("username", null);
hand.post(r2);
}
});
mm_back_btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {
// TODO Auto-generated method stub
Comment_count.this.finish();
}
});
comment_countent = (LinearLayout) findViewById(R.id.comment_countent);
Intent intent = getIntent();
ctt = (Countent) intent.getSerializableExtra("count");
System.out.println("评论留言" + ctt);
cmm_title.setText(ctt.getName());
cmm_content.setText(ctt.getCount());
cmm_time.setText(ctt.getCounttime());
hand.postDelayed(r,3000);
} private Handler hand = new Handler() { @Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.arg1) {
case InitConfig.loaderr:
TextView t = new TextView(Comment_count.this);
t.setText("无");
comment_countent.addView(t);
break;
case InitConfig.showlistview:
if (listres != null) {
Comment_count_item cv = (Comment_count_item) msg.obj;
if (cv != null)
comment_countent.addView(cv);
}
break;
case InitConfig.suberr:
Toast.makeText(Comment_count.this, "发表失败,请检查网络连接!", Toast.LENGTH_LONG).show();
InputMethodManager imm1 = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
//得到InputMethodManager的实例
if (imm1.isActive()) {
//如果开启
imm1.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
//关闭软键盘,开启方法相同,这个方法是切换开启与关闭状态的
}
break;
case InitConfig.subok:
Toast.makeText(Comment_count.this, "发表成功!", Toast.LENGTH_LONG).show();
hand.post(r);
cvc_et.setText("");
InputMethodManager imm2 = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
//得到InputMethodManager的实例
if (imm2.isActive()) {
//如果开启
imm2.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
//关闭软键盘,开启方法相同,这个方法是切换开启与关闭状态的
}
break;
case InitConfig.subno:
Toast.makeText(Comment_count.this, "发表失败!", Toast.LENGTH_LONG).show();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
//得到InputMethodManager的实例
if (imm.isActive()) {
//如果开启
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
//关闭软键盘,开启方法相同,这个方法是切换开启与关闭状态的
}
break;
default:
break;
}
super.handleMessage(msg);
} };
Runnable r2 = new Runnable() { public void run() {
// TODO Auto-generated method stub
if (ctt != null) {
JSONObject jo=new JSONObject();
try {
jo.put("act","insercomment");
if(etcun!=null)
jo.put("comment", etcun);
if(userstr!=null){
jo.put("name", userstr);
}else{
jo.put("name", "游客");
}
jo.put("countentid", ctt.getId());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String result = hu.postHttpClientString(
InitConfig.https, jo.toString());
if (result.equals("err")) {
Message msg2 = new Message();
msg2.arg1 = InitConfig.suberr;
hand.sendMessage(msg2);
} else if (result.equals("errnull")) {
Message msg2 = new Message();
msg2.arg1 = InitConfig.suberr;
hand.sendMessage(msg2);
} else {
Map maps = JsonUtils.stringtomap(result);
if ((Boolean) maps.get("result")) {
Message msg2 = new Message();
msg2.arg1 = InitConfig.subok;
hand.sendMessage(msg2);
}else{
Message msg2 = new Message();
msg2.arg1 = InitConfig.subno;
hand.sendMessage(msg2);
}
}
}
}
}; Runnable r = new Runnable() {
public void run() {
if (ctt != null) {
// TODO Auto-generated method stub
JSONObject js = new JSONObject();
try {
js.put("act", "fandcomment");
js.put("startsize", 0);
js.put("countentid", ctt.getId());
js.put("endsize", number);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} String result = hu.postHttpClientString(
InitConfig.https, js.toString());
if (result.equals("err")) {
Message msg2 = new Message();
msg2.arg1 = InitConfig.loaderr;
hand.sendMessage(msg2);
} else if (result.equals("errnull")) {
Message msg2 = new Message();
msg2.arg1 = InitConfig.loaderr;
hand.sendMessage(msg2);
} else {
if (listres != null) {
listres.clear();
listres = null;
}
Map maps = JsonUtils.stringtomap(result);
listres = JsonUtils.stringjsontomap2(maps.get("obj")
.toString());
System.out.println("ArrayList<Comment>==" + listres);
for (int i = 0; i < listres.size(); i++) { Message msg2 = new Message();
Comment_count_item v = new Comment_count_item(
Comment_count.this, listres.get(i));
msg2.obj = v;
msg2.arg1 = InitConfig.showlistview;
hand.sendMessage(msg2); }
} }
}
};
}
/*
* 根据访问地址返回访问结果 err代表网络连接失败 null代表用户账号或者密码错误 其他代表成功
*/
public String postHttpClientString(String urlstr, String st) {
System.out.println("访问地址=" + urlstr+"?act="+st);
String result = null;
StringBuilder sb = new StringBuilder();
HttpURLConnection con = null;
String line = null;
BufferedReader buffer = null;
try {
URL url = new URL(urlstr);
con = (HttpURLConnection) url.openConnection();
// 连接10秒
con.setConnectTimeout(5000);
con.setRequestMethod("POST");
// 允许写入参数
con.setDoOutput(true);
// con.setDoInput(true);
con.setInstanceFollowRedirects(true);
// 读取数据10秒
con.setReadTimeout(10000);
// 请求不能使用缓存
//con.setUseCaches(false);
// 设定传送的内容类型是可序列化的java对象
// (如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException) // 写入数据
if (st != null) {
DataOutputStream conout = new DataOutputStream(con.getOutputStream()); conout.write(("act=" + st).getBytes());
conout.flush();
conout.close();
}
InputStream input = con.getInputStream();
if (input != null) {
buffer = new BufferedReader(new InputStreamReader(input));
while ((line = buffer.readLine()) != null) {
sb.append(line.trim());
}
result = sb.toString();
} else {
System.out.println("连接失败");
result = "errnull";
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("出现错误1");
result = "err";
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("出现错误2");
e.printStackTrace();
result = "err";
} finally {
if (buffer != null) {
try {
buffer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (con != null) {
con.disconnect();
con=null;
}
}
System.out.println("web返回" + result);
return result;
}
是说你在主线程中访问了网络,解决办法:将访问网络的代码单独放到一个线程中
http://blog.csdn.net/aminfo/article/details/7903112
你可以去掉检查
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.
Builder().detectDiskReads().detectDiskWrites().
detectNetwork().penaltyLog().build());或者网络访问新起一个线程