java.io.FileNotFoundException:""在libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:194)

时间:2017-05-31 06:32:10

标签: android post httpurlconnection

我有一个API可以将图像和其他详细信息发布到服务器。我得到.as-console-wrapper { max-height: 100% !important; top: 0; }。请找到以下代码:

FileNotFoundException

控件转到第二个public int uploadFile(final String sourceFileUri) { String fileName = sourceFileUri; 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; Log.d("uploadFile","sourceFileURI:: "+sourceFileUri); File sourceFile = new File(sourceFileUri); int serverResponseCode = 0; try { FileInputStream fileInputStream = new FileInputStream(sourceFile.getAbsoluteFile()); URL url = new URL("http://samaya.apprikart.com/dd_api_v1/report_news/"); conn = (HttpURLConnection) url.openConnection(); 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;boundary="+boundary); dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"news_title\"" + lineEnd); dos.writeBytes(lineEnd); dos.writeBytes("NEWS TITLE"); // mobile_no is String variable dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"image_link\";filename=\""+ mSelectedImagePath + "\"" + lineEnd); dos.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; 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); } dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); serverResponseCode = conn.getResponseCode(); String serverResponseMessage = conn.getResponseMessage(); Log.d("uploadFile", "HTTP Response is : "+ serverResponseMessage + ": " + serverResponseCode); final StringBuffer response = new StringBuffer(); InputStream is = conn.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; while((line = rd.readLine()) != null) { response.append(line); response.append('\r'); } rd.close(); Log.d("uploadFile", "response.toString(): "+response.toString()); if (serverResponseCode == 200) { if (getActivity()!=null) getActivity().runOnUiThread(new Runnable() { public void run() { Toast.makeText(mContext, "Submitted Successfully.", Toast.LENGTH_LONG) .show(); if (getActivity()!=null) getActivity().finish(); } }); }else{ Log.d("uploadFile", "error.toString(): "+conn.getErrorStream()); } fileInputStream.close(); dos.flush(); dos.close(); } catch (MalformedURLException ex) { ex.printStackTrace(); if (getActivity()!=null) getActivity().runOnUiThread(new Runnable() { public void run() { Toast.makeText(mContext, "Uh Oh! Error occured. Please try later", Toast.LENGTH_SHORT) .show(); } }); Log.d("Upload file to server", "error: " + ex.getMessage(), ex); } catch (final Exception e) { e.printStackTrace(); if (getActivity()!=null) getActivity().runOnUiThread(new Runnable() { public void run() { Toast.makeText(mContext, "Uh Oh! Error occured. Please try later", Toast.LENGTH_SHORT).show(); } }); Log.d("Upload file to server Exception", "Exception : " + e.getMessage(), e); } return serverResponseCode; } 块。我已将URL_HERE替换为我的网址。我在异步任务中调用此catch方法。我也对我的另一个API做了同样的方法。它在那里工作正常。但在这里,它给出了一个错误。我在这做什么错?

来自Logcat的消息:

例外:http://samaya.apprikart.com/dd_api_v1/report_news/                                                                                  java.io.FileNotFoundException:http://samaya.apprikart.com/dd_api_v1/report_news/                                                                                      在libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:194)                                                                                      在com.FryingPan.fragments.AddRecipeFragment.uploadFile(AddRecipeFragment.java:735)                                                                                      在com.FryingPan.fragments.AddRecipeFragment $ SubmitToServer.doInBackground(AddRecipeFragment.java:520)                                                                                      在com.TheFryingPan.fragments.AddRecipeFragment $ SubmitToServer.doInBackground(AddRecipeFragment.java:495)                                                                                      在android.os.AsyncTask $ 2.call(AsyncTask.java:287)                                                                                      在java.util.concurrent.FutureTask.run(FutureTask.java:234)                                                                                      在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230)                                                                                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)                                                                                      at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573)                                                                                      在java.lang.Thread.run(Thread.java:838)

0 个答案:

没有答案