Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 虽然文件存在,但找不到AsyncTask返回文件_Android_Android Asynctask - Fatal编程技术网

Android 虽然文件存在,但找不到AsyncTask返回文件

Android 虽然文件存在,但找不到AsyncTask返回文件,android,android-asynctask,Android,Android Asynctask,我在Android Studio中遇到了AsyncTask类问题。当文件确实存在时,它不断给我消息“文件未找到”。下面是我的代码片段: public class PhpInsertProduct extends AsyncTask<String, Void, String> { private BufferedReader bufferedReader; private BufferedWriter bufferedWriter; private Conte

我在Android Studio中遇到了
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();
        }
    }
}
公共类PhpInsertProduct扩展异步任务{
私有BufferedReader BufferedReader;
专用缓冲写入程序缓冲写入程序;
私人语境;
私有HttpURLConnection HttpURLConnection;
私有输出流输出流;
私有进程对话;
私人快餐店;
私有静态字符串PHP_INSERT_PRODUCT=”http://www.example.com/phpexample.php“;//此文件存在,但Android Studio不断为我提供此文件的“未找到文件”错误
私有字符串inputLine、query、result、categoryName、name、price、image、saleStartTime、saleDuration、salePrice、desc、createdAt、createdBy;
私有字符串[]ftpimagepath;
私有缓冲响应;
私有文本视图tvSnackbarTextView;
私有Uri.Builder-uriBuilder;
私有URL;
私有视图snackbar视图,视图;
公共PhpInsertProduct(上下文上下文、视图视图、进度对话框、字符串categoryName、字符串[]ftpimagepath)
{
this.context=上下文;
this.view=视图;
this.progressDialog=progressDialog;
this.categoryName=categoryName;
this.ftpimagepath=ftpimagepath;
}
@凌驾
受保护的字符串doInBackground(字符串…args)
{
name=args[0];
价格=args[1];
image=args[2];
saleStartTime=args[3];
saleDuration=args[4];
salePrice=args[5];
desc=args[6];
createdAt=args[7];
createdBy=args[8];
尝试
{
url=新url(PHP\u插入\u产品);
httpURLConnection=(httpURLConnection)url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod(“POST”);
httpURLConnection.addRequestProperty(“接受编码”、“UTF-8”);
uriBuilder=新的Uri.Builder()
.appendQueryParameter(“名称”,名称)
.appendQueryParameter(“价格”,价格)
.appendQueryParameter(“图像”,图像)
.appendQueryParameter(“saleStartTime”,saleStartTime)
.appendQueryParameter(“saleDuration”,saleDuration)
.appendQueryParameter(“salePrice”,salePrice)
.附录查询参数(“描述”,描述)
.appendQueryParameter(“createdAt”,createdAt)
.appendQueryParameter(“createdBy”,createdBy);
query=uriBuilder.build().getEncodedQuery();
outputStream=httpURLConnection.getOutputStream();
bufferedWriter=新的bufferedWriter(新的OutputStreamWriter(outputStream,UTF-8));
bufferedWriter.write(查询);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
httpURLConnection.connect();
bufferedReader=new bufferedReader(new InputStreamReader(httpURLConnection.getInputStream());//这是触发错误的行
响应=新的StringBuffer();
while((inputLine=bufferedReader.readLine())!=null)response.append(inputLine);
bufferedReader.close();
结果=response.toString();
httpURLConnection.disconnect();
}
捕获(例外e)
{
e、 printStackTrace();
返回e.getMessage();
}
返回结果;
}
@凌驾
受保护的void onPostExecute(字符串结果)
{
if(result.isEmpty())新的PhpGetItemId(上下文、视图、progressDialog、categoryName、ftPimagePath、null、createdAt、createdBy)。executeOnExecutor(AsyncTask.THREAD\u POOL\u EXECUTOR,名称);
其他的
{
progressDialog.disclose();
FragmentAddProducts.FragmentAddProducts.setSaveInProgress(假);
snackbar=snackbar.make(视图,context.getResources().getString(R.string.error\u saving\u product),snackbar.LENGTH\u 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类,它们工作得很好。我只是在这门课上犯了错误,我不知道为什么。
有人知道如何解决这个问题吗?

我发现问题实际上是服务器端的。服务器的PHP语言版本较低,因此PHP脚本无法在服务器上正常运行。但是,在移动到另一个具有更高PHP版本的服务器后,脚本工作正常。问题解决了


然而,非常不幸的是,Android Studio给出了误导性的错误消息。它说文件在那里时找不到。它应该给出更合适的错误消息,例如:PHP错误或类似的错误。

我发现问题实际上是服务器端的。服务器的PHP语言版本较低,因此PHP脚本无法在服务器上正常运行。但是,在移动到另一个具有更高PHP版本的服务器后,脚本工作正常。问题解决了


然而,非常不幸的是,Android Studio给出了误导性的错误消息。它说文件在那里时找不到。它应该给出更合适的错误消息,例如:PHP错误或类似错误。

请共享您的实际URL请与错误一起共享logcat请与错误一起共享您的实际URL请与错误一起共享logcat