Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.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
Php Android:无法从服务器获取JSON数组_Php_Android_Arrays_Json_Server - Fatal编程技术网

Php Android:无法从服务器获取JSON数组

Php Android:无法从服务器获取JSON数组,php,android,arrays,json,server,Php,Android,Arrays,Json,Server,我正在做一个个人项目,我需要向数据库发送一些数据,数据库应该返回详细信息(JSON格式)。但是,我的应用程序没有收到来自服务器的任何响应(它返回空值)。有人能帮我吗?谢谢以下是我的代码: import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Context; import android.content.Inten

我正在做一个个人项目,我需要向数据库发送一些数据,数据库应该返回详细信息(JSON格式)。但是,我的应用程序没有收到来自服务器的任何响应(它返回空值)。有人能帮我吗?谢谢以下是我的代码:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.concurrent.RecursiveTask;

public class InventoryBackground extends AsyncTask<String, Void, String> {

String inventory_url = "http://10.100.50.19/inventory.php";


Context ctx;
ProgressDialog progressDialog;
Activity activity;
AlertDialog.Builder builder;

public InventoryBackground(Context ctx){
    this.ctx = ctx;
    activity = (Activity) ctx;
}

@Override
protected void onPreExecute() {
    builder = new AlertDialog.Builder(activity);
    progressDialog = new ProgressDialog(ctx);
    progressDialog.setTitle("Please Wait");
    progressDialog.setMessage("Searching From Database...");
    progressDialog.setIndeterminate(true);
    progressDialog.setCancelable(false);
    progressDialog.show();
}

@Override
protected String doInBackground(String... params) {

    try{
        URL url = new URL(inventory_url);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        OutputStream outputStream = httpURLConnection.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));

        String part_id;

        part_id = params[0];


        String data = URLEncoder.encode("partid","UTF-8") +"="+ URLEncoder.encode(part_id,"UTF-8");


        bufferedWriter.write(data);
        bufferedWriter.flush();
        bufferedWriter.close();
        outputStream.close();

        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        StringBuilder stringBuilder = new StringBuilder();
        String line = "";
        while((line = bufferedReader.readLine()) != null)
        {
            stringBuilder.append(line + "\n");
        }

        httpURLConnection.disconnect();
        Thread.sleep(5000);
        return stringBuilder.toString().trim();


    }


    catch (MalformedURLException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onProgressUpdate(Void... values) { super.onProgressUpdate(values); }

@Override
protected void onPostExecute(String json) {


    try{
        progressDialog.dismiss();

        JSONObject jsonObject = new JSONObject(json);
        JSONArray jsonArray = jsonObject.getJSONArray("server_response");

        JSONObject JO = jsonArray.getJSONObject(0);

        String code = JO.getString("code");
        System.out.println("Code = " + code);
        String message_inv_partno = JO.getString("message_inv_partno");

        if (code.equals("partid_true"))
        {

            TextView partID = (TextView) activity.findViewById(R.id.tv_inventory_data_partno);



            partID.setText(message_inv_partno);

        }

        else if (code.equals("partid_false")) {


        }

    }
    catch (JSONException e) {
        e.printStackTrace();
    }





    }
}
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.ProgressDialog;
导入android.content.Context;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.widget.TextView;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.io.BufferedInputStream;
导入java.io.BufferedReader;
导入java.io.BufferedWriter;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.io.OutputStream;
导入java.io.OutputStreamWriter;
导入java.net.HttpURLConnection;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.net.urlcoder;
导入java.util.concurrent.RecursiveTask;
公共类InventoryBackground扩展异步任务{
字符串资源清册\u url=”http://10.100.50.19/inventory.php";
上下文ctx;
进行对话进行对话;
活动;
AlertDialog.Builder;
公共资源清册背景(上下文ctx){
this.ctx=ctx;
活动=(活动)ctx;
}
@凌驾
受保护的void onPreExecute(){
builder=新建AlertDialog.builder(活动);
progressDialog=新的progressDialog(ctx);
progressDialog.setTitle(“请稍候”);
setMessage(“从数据库中搜索…”);
progressDialog.setUndeterminate(true);
progressDialog.setCancelable(假);
progressDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
URL=新的URL(目录URL);
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod(“POST”);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream OutputStream=httpURLConnection.getOutputStream();
BufferedWriter BufferedWriter=新的BufferedWriter(新的OutputStreamWriter(outputStream,UTF-8));
字符串部分id;
part_id=params[0];
字符串数据=urlcoder.encode(“partid”,“UTF-8”)+“=”+urlcoder.encode(part_id,“UTF-8”);
bufferedWriter.write(数据);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream InputStream=httpURLConnection.getInputStream();
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(inputStream));
StringBuilder StringBuilder=新的StringBuilder();
字符串行=”;
而((line=bufferedReader.readLine())!=null)
{
stringBuilder.append(行+“\n”);
}
httpURLConnection.disconnect();
睡眠(5000);
返回stringBuilder.toString().trim();
}
捕获(格式错误){
e、 printStackTrace();
}
捕获(IOE异常){
e、 printStackTrace();
}
捕捉(中断异常e){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onProgressUpdate(void…values){super.onProgressUpdate(values);}
@凌驾
受保护的void onPostExecute(字符串json){
试一试{
progressDialog.disclose();
JSONObject JSONObject=新的JSONObject(json);
JSONArray JSONArray=jsonObject.getJSONArray(“服务器响应”);
JSONObject JO=jsonArray.getJSONObject(0);
字符串代码=JO.getString(“代码”);
System.out.println(“Code=“+Code”);
String message_inv_partno=JO.getString(“message_inv_partno”);
if(代码等于(“partid_true”))
{
TextView partID=(TextView)activity.findViewById(R.id.tv\u inventory\u data\u partno);
partID.setText(信息库存零件号);
}
else if(代码等于(“partid_false”)){
}
}
捕获(JSONException e){
e、 printStackTrace();
}
}
}

您的服务器在使用postmanAh时是否正确响应,已解决,谢谢!!