Android HttpUrlConnection.connect引发异常
在AndroidStudio中开发,我有以下代码来连接并将一些数据发布到服务器:Android HttpUrlConnection.connect引发异常,android,android-studio,connect,httpurlconnection,Android,Android Studio,Connect,Httpurlconnection,在AndroidStudio中开发,我有以下代码来连接并将一些数据发布到服务器: try { EditText et = (EditText)findViewById(R.id.friend); EditText et2 = (EditText)findViewById(R.id.name); HashMap<String,String> hm = new HashMap<>();
try {
EditText et = (EditText)findViewById(R.id.friend);
EditText et2 = (EditText)findViewById(R.id.name);
HashMap<String,String> hm = new HashMap<>();
hm.put("name", et2.getText().toString());
hm.put("friend_name", et.getText().toString());
URL url = new URL("http://wwtbamandroid.appspot.com/rest/friends");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.connect();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream(), "UTF-8"));
writer.write(getPostDataString(hm));
writer.flush();
writer.close();
}
catch (Exception e){
Log.e("Error", "Connection error -> "+e.getMessage());}
试试看{
EditText et=(EditText)findViewById(R.id.friend);
EditText et2=(EditText)findViewById(R.id.name);
HashMap hm=新的HashMap();
hm.put(“name”,et2.getText().toString());
hm.put(“friend_name”,et.getText().toString());
URL=新URL(“http://wwtbamandroid.appspot.com/rest/friends");
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.setRequestMethod(“POST”);
con.设置输出(真);
con.connect();
BufferedWriter writer=新的BufferedWriter(新的OutputStreamWriter(con.getOutputStream(),“UTF-8”);
write(getPostDataString(hm));
writer.flush();
writer.close();
}
捕获(例外e){
Log.e(“错误”,“连接错误->”+e.getMessage());}
并且在con.connect()行中抛出一个异常;我不明白为什么,也找不到错误。谢谢你的帮助
这是我的日志:
08-02 21:15:07.798 16864-16864/? I/art﹕ Late-enabling -Xcheck:jni
08-02 21:15:08.070 16864-16881/android.josema.whowantstobeamillionaire I/art﹕ Background partial concurrent mark sweep GC freed 187(24KB) AllocSpace objects, 0(0B) LOS objects, 31% free, 35MB/51MB, paused 6.048ms total 24.693ms
08-02 21:15:08.091 16864-16897/android.josema.whowantstobeamillionaire D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
08-02 21:15:08.096 16864-16864/android.josema.whowantstobeamillionaire D/Atlas﹕ Validating map...
08-02 21:15:08.149 16864-16897/android.josema.whowantstobeamillionaire I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 01/14/15, ab0075f, Id3510ff6dc
08-02 21:15:08.150 16864-16897/android.josema.whowantstobeamillionaire I/OpenGLRenderer﹕ Initialized EGL, version 1.4
08-02 21:15:08.163 16864-16897/android.josema.whowantstobeamillionaire D/OpenGLRenderer﹕ Enabling debug mode 0
08-02 21:15:15.181 16864-16864/android.josema.whowantstobeamillionaire
E/Error﹕ Connection error -> null
08-0221:15:07.79816864-16864/?I/艺术﹕ 延迟启用-Xcheck:jni
08-02 21:15:08.070 16864-16881/android.josema.who wantsbeamillionaire I/art﹕ 背景部分并发标记扫描GC释放187(24KB)AllocSpace对象,0(0B)LOS对象,31%空闲,35MB/51MB,暂停6.048ms总计24.693ms
08-02 21:15:08.091 16864-16897/android.josema.who想成为百万富翁的D/opengl﹕ 使用EGL\u交换\u行为\u保留:true
08-02 21:15:08.096 16864-16864/android.josema.who wantsbeamillionaire D/Atlas﹕ 正在验证映射。。。
08-02 21:15:08.149 16864-16897/android.josema.who wantsbeamillionaire I/Adreno EGL﹕ : 高通公司生产:2015年1月14日,ab0075f,Id3510ff6dc
08-02 21:15:08.150 16864-16897/android.josema.who wantsbeamillionaire I/opengl﹕ 已初始化EGL,版本1.4
08-02 21:15:08.163 16864-16897/android.josema.who想成为百万富翁的D/opengl﹕ 正在启用调试模式0
08-02 21:15:15.181 16864-16864/android.josema.who wantsbeamillionaire
E/错误﹕ 连接错误->空
使用这行代码
Thread background = new Thread(new Runnable() {
// After call for background.start this run method call
public void run() {
try {
HashMap<String,String> hm = new HashMap<>();
hm.put("name", et2.getText().toString());
hm.put("friend_name", et.getText().toString());
URL url = new URL("http://wwtbamandroid.appspot.com/rest/friends");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.connect();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream(), "UTF-8"));
writer.write(getPostDataString(hm));
writer.flush();
writer.close();
} catch (Throwable t) {
// just end the background thread
Log.i("Animation", "Thread exception " + t); }
}
});
// Start Thread
background.start();
Thread background=新线程(new Runnable()){
//在后台调用之后。启动此运行方法调用
公开募捐{
试一试{
HashMap hm=新的HashMap();
hm.put(“name”,et2.getText().toString());
hm.put(“friend_name”,et.getText().toString());
URL=新URL(“http://wwtbamandroid.appspot.com/rest/friends");
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.setRequestMethod(“POST”);
con.设置输出(真);
con.connect();
BufferedWriter writer=新的BufferedWriter(新的OutputStreamWriter(con.getOutputStream(),“UTF-8”);
write(getPostDataString(hm));
writer.flush();
writer.close();
}捕获(可丢弃的t){
//只需结束背景线程
Log.i(“动画”,“线程异常”+t);}
}
});
//起始线程
background.start();
例外情况是什么?是NetworkOnMainThread异常吗?你也可以发布日志吗你的连接是空的检查这样的连接是否存在如果(con==null)Log.d(“null”,“你的连接是空的”);如果(con!=null)Log.d(“null”,“您的连接不是null”);而且它不是空的