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連同錯誤 –