机器人 - HttpConnection的错误,而上传图像到服务器
我试图将图像发送到我的PHP服务器,但我得到这些错误机器人 - HttpConnection的错误,而上传图像到服务器
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1144)
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 libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
这里是我的发送图像
public int uploadFile(String sourceFileUri) {
System.out.println("file path is " + sourceFileUri); //here i am successfully getting the image path
String upLoadServerUri = "http://www";
String fileName = sourceFileUri;
int serverResponseCode = 0;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile()) {
Log.e("uploadFile", "Source File Does not exist");
return 0;
}
try { // open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection(); // Open a HTTP
// connection to
// the URL
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available(); // create a buffer of
// maximum size
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage
+ ": " + serverResponseCode);
if (serverResponseCode == 200) {
System.out.println("server is ok");
}
// close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
e.printStackTrace();
Log.e("Upload file to server Exception",
"Exception : " + e.getMessage(), e);
}
return serverResponseCode;
}
的代码我不想使用异步任务。请给我一些其他的方式来发送图像,你可以做一个
双向使用ASYN任务
public class MyUploadTask extends AsynTask<void,void,void>{
void doInBackground(){
//... do your upload task
}
}
另外,如果你想发布更新的UI在一个正常的线程中使用android.os.Handler OB为使用线程
Thread myUploadTask = new Thread(new Runnable(){
void run(){
//... do your upload task
}
});
myUploadTask.start();
在主线程和工作线程之间发送消息。
你的情况: 如果我想发送图像到服务器onCreate()方法
public void onCreate(){
super.onCreate();
uploadToServer();
}
public void uploadToServer(){
Thread myUploadTask = new Thread (new Runnable(){
void run(){
//Calling the upload Image method
uploadFile("www.yourdomin.com/uploadFile");
}
});
myUploadTask.start();
}
你可以请编辑我的代码,并告诉我应该在哪里写你的代码。我是Android新手。对不起 – hellosheikh
这个异常是因为你在主线程上传的图片而是使用AsyncTask
上传图像到服务器...
我不想使用异步任务。请你能告诉我其他方式发送图片 – hellosheikh
为什么???????????? –
,因为它需要很长的后台处理,这就是为什么。我想要一些更好的方式来做到这一点 – hellosheikh
检查此[链接] [1]它可以帮助你...... [1]:http://stackoverflow.com/questions/6976317/android-http-connection-exception –
另一个NetworkOnMainThreadException问题,真的吗?下次使用Google ... – 2Dee