Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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
Java 读取从服务器发送的数据时为空字符串_Java_Php_Android_Json - Fatal编程技术网

Java 读取从服务器发送的数据时为空字符串

Java 读取从服务器发送的数据时为空字符串,java,php,android,json,Java,Php,Android,Json,我的代码有问题。变量result应该从服务器的响应中填充,但由于任何原因,它会不断返回空字符串 这是全部代码: public class FragmentRally extends Fragment { public FragmentRally() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup containe

我的代码有问题。变量result应该从服务器的响应中填充,但由于任何原因,它会不断返回空字符串

这是全部代码:

public class FragmentRally extends Fragment {

public FragmentRally() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_fragment_rally, container, false);


    new AsyncFetch().execute();


    return rootView;
}


// CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
public static final int CONNECTION_TIMEOUT = 10000;
public static final int READ_TIMEOUT = 15000;
private RecyclerView vistaRallye;
private AdapterRallye adaptadorRallye;

private class AsyncFetch extends AsyncTask<String, String, String> {
    ProgressDialog pdLoading = new ProgressDialog(getActivity());
    HttpURLConnection conn;
    URL url = null;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        //this method will be running on UI thread
        pdLoading.setMessage("\tCarregant...");
        pdLoading.setCancelable(false);
        pdLoading.show();

    }

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

            // Enter URL address where your json file resides
            // Even you can make call to php file which returns json data
            url = new URL("http://www.rallyecat.esy.es/Obtenir_events.php");

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return e.toString();
        }
        try {

            // Setup HttpURLConnection class to send and receive data from php and mysql
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("GET");

            // setDoOutput to true as we recieve data from json file
            conn.setDoOutput(true);

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return e1.toString();
        }

        try {

            int response_code = conn.getResponseCode();

            // Check if successful connection made
            if (response_code == HttpURLConnection.HTTP_OK) {

                // Read data sent from server
                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder result = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }

                // Pass data to onPostExecute method
                return (result.toString());

            } else {

                return ("No hi ha connexió a internet.");
            }

        } catch (IOException e) {
            e.printStackTrace();
            return e.toString();
        } finally {
            conn.disconnect();
        }


    }

    @Override
    protected void onPostExecute(String result) {

        //this method will be running on UI thread

        pdLoading.dismiss();
        List<DataRallye> data = new ArrayList<>();

        pdLoading.dismiss();
        try {
            JSONArray jArray = new JSONArray(result);

            // Extract data from json and store into ArrayList as class objects
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                DataRallye dadesrallye = new DataRallye();
                dadesrallye.RallyeNom = json_data.getString("nom");
                dadesrallye.RallyeTipus = json_data.getString("tipus");
                dadesrallye.RallyeDataI = json_data.getString("datai");
                dadesrallye.RallyeDataF = json_data.getString("dataf");
                dadesrallye.RallyeCiutat = json_data.getString("ciutat");
                dadesrallye.RallyeOrganitzacio = json_data.getString("organitzacio");
                dadesrallye.RallyeFoto = json_data.getString("foto");
                data.add(dadesrallye);
            }

            // Setup and Handover data to recyclerview
            vistaRallye = (RecyclerView) getView().findViewById(R.id.llistarallyes);
            adaptadorRallye = new AdapterRallye(getActivity(), data);
            vistaRallye.setAdapter(adaptadorRallye);
            vistaRallye.setLayoutManager(new LinearLayoutManager(getActivity()));

        } catch (JSONException e) {
            Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG).show();
        }

    }

}
这个变量RESULT传递到这里,我在这里进行JSON解析。但由于它是空的,因此直接转到catch异常:

@Override
    protected void onPostExecute(String result) {

        //this method will be running on UI thread

        pdLoading.dismiss();
        List<DataRallye> data = new ArrayList<>();

        pdLoading.dismiss();
        try {
            JSONArray jArray = new JSONArray(result);

            // Extract data from json and store into ArrayList as class objects
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                DataRallye dadesrallye = new DataRallye();
                dadesrallye.RallyeNom = json_data.getString("nom");
                dadesrallye.RallyeTipus = json_data.getString("tipus");
                dadesrallye.RallyeDataI = json_data.getString("datai");
                dadesrallye.RallyeDataF = json_data.getString("dataf");
                dadesrallye.RallyeCiutat = json_data.getString("ciutat");
                dadesrallye.RallyeOrganitzacio = json_data.getString("organitzacio");
                dadesrallye.RallyeFoto = json_data.getString("foto");
                data.add(dadesrallye);
            }

            // Setup and Handover data to recyclerview
            vistaRallye = (RecyclerView) getView().findViewById(R.id.llistarallyes);
            adaptadorRallye = new AdapterRallye(getActivity(), data);
            vistaRallye.setAdapter(adaptadorRallye);
            vistaRallye.setLayoutManager(new LinearLayoutManager(getActivity()));

        } catch (JSONException e) {
            Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG).show();
        }
拆下那条线。因为您不会将数据写入outputstream

result.append(line); 
那应该是

result.append(line) + "\n"; 

对于“获取”连接集

 conn.setDoOutput(false);

setDoOutputtrue用于POST和PUT请求

conn.SETDOOUTPUTRUE;。删除该行。result.appendline;应该是result.appendline+\n@绿色应用解决了我的问题!多谢各位!这条线在干什么?谢谢返回No hi ha connexióa internet。;错误的您的结果代码与“确定”不同。说出密码。
result.append(line); 
result.append(line) + "\n"; 
 conn.setDoOutput(false);