1
我在Android Studio中遇到AsyncTask
类的问题。当文件确实存在时,它会一直给我发送“找不到文件”的消息。以下是我的代码片段:虽然存在文件,但找不到AsyncTask返回文件
public class PhpInsertProduct extends AsyncTask<String, Void, String> {
private BufferedReader bufferedReader;
private BufferedWriter bufferedWriter;
private Context context;
private HttpURLConnection httpURLConnection;
private OutputStream outputStream;
private ProgressDialog progressDialog;
private Snackbar snackbar;
private static String PHP_INSERT_PRODUCT = "http://www.example.com/phpexample.php"; //this file exists but Android Studio keeps giving me "file not found" error for this file
private String inputLine, query, result, categoryName, name, price, image, saleStartTime, saleDuration, salePrice, desc, createdAt, createdBy;
private String[] ftpImagePaths;
private StringBuffer response;
private TextView tvSnackbarTextView;
private Uri.Builder uriBuilder;
private URL url;
private View snackbarView, view;
public PhpInsertProduct(Context context, View view, ProgressDialog progressDialog, String categoryName, String[] ftpImagePaths)
{
this.context = context;
this.view = view;
this.progressDialog = progressDialog;
this.categoryName = categoryName;
this.ftpImagePaths = ftpImagePaths;
}
@Override
protected String doInBackground(String... args)
{
name = args[0];
price = args[1];
image = args[2];
saleStartTime = args[3];
saleDuration = args[4];
salePrice = args[5];
desc = args[6];
createdAt = args[7];
createdBy = args[8];
try
{
url = new URL(PHP_INSERT_PRODUCT);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.addRequestProperty("Accept-Encoding", "UTF-8");
uriBuilder = new Uri.Builder()
.appendQueryParameter("name", name)
.appendQueryParameter("price", price)
.appendQueryParameter("image", image)
.appendQueryParameter("saleStartTime", saleStartTime)
.appendQueryParameter("saleDuration", saleDuration)
.appendQueryParameter("salePrice", salePrice)
.appendQueryParameter("desc", desc)
.appendQueryParameter("createdAt", createdAt)
.appendQueryParameter("createdBy", createdBy);
query = uriBuilder.build().getEncodedQuery();
outputStream = httpURLConnection.getOutputStream();
bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
bufferedWriter.write(query);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
httpURLConnection.connect();
bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); //this is the line that triggers the error
response = new StringBuffer();
while ((inputLine = bufferedReader.readLine()) != null) response.append(inputLine);
bufferedReader.close();
result = response.toString();
httpURLConnection.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
return e.getMessage();
}
return result;
}
@Override
protected void onPostExecute(String result)
{
if(result.isEmpty()) new PhpGetItemId(context, view, progressDialog, categoryName, ftpImagePaths, null, createdAt, createdBy).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, name);
else
{
progressDialog.dismiss();
FragmentAddProducts.fragmentAddProducts.setSaveInProgress(false);
snackbar = Snackbar.make(view, context.getResources().getString(R.string.error_saving_product), Snackbar.LENGTH_LONG);
snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.parseColor("#00DDFF"));
tvSnackbarTextView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
tvSnackbarTextView.setTextColor(Color.parseColor("#FF3333"));
snackbar.show();
}
}
}
仅供参考,我有很多类似于此的其他AsyncTask类,他们工作得很好。我只是为这个班级出错,我不知道为什么。 有谁知道如何真正解决这个问题?
请分享您的实际网址 –
请分享logcat连同错误 –