【问题标题】:Android JSON to PHP serverAndroid JSON 到 PHP 服务器
【发布时间】:2023-03-05 08:24:01
【问题描述】:

我正在尝试将所有数据从 SQLite 发送到 PHP MySQL。我制作了一个 JSON 对象来将数据发送到 PHP。我在 PHP 端没有收到任何数据。

安卓代码

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


    try {
        String link = "http://localhost/Myapp/course.php";

        handler.open();
        Cursor c = handler.returnData();
        if (c.getCount() == 0) {
            Toast.makeText(context, "No Data Found", Toast.LENGTH_LONG).show();
        }

        obj = new JSONObject();
        while (c.moveToNext()) {
            String cid = c.getString(0);
            String name = c.getString(1);
            obj.put("cid", Integer.parseInt(cid));
            obj.put("cname", name);
        }
        handler.close();
        array = new JSONArray();
        array.put(obj);

        sendObj = new JSONObject();
        sendObj.put("course", array);

        String data = sendObj.toString();

        URL url = new URL(link);
        URLConnection conn = url.openConnection();

        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

        wr.write(data);
        wr.flush();

        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        StringBuilder sb = new StringBuilder();
        String line = null;

        // Read Server Response
        while ((line = reader.readLine()) != null) {
            sb.append(line);
            break;
        }
        return sb.toString();
    } catch (MalformedURLException e) {
    } catch (UnsupportedEncodingException e) {
    } catch (IOException e) {
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();


    }
    return null;
}    

PHP 代码
有没有其他方法来解码 json 数据,或者我应该在我的 android 代码中更改一些内容?

<?php  
 require_once("dbconnect.inc");  
 $data = array();  
 $data = json_decode($_POST["course"]);  
 $cid=$data->cid;  
 $cname=$data->cname;  
 mysql_query("insert into COURSE values($cid,$cname)") or die(mysql_error());  
?>

【问题讨论】:

  • 旁注:您是否希望查询中只有一条结果记录?您正在构建一个 JSONArray,但它只包含一个元素。
  • 我想把SQLite中的所有数据都存储到MySql中

标签: php android mysql json


【解决方案1】:

您需要为URlConnection设置参数。

试试这个:

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


    try {
        String link = "http://localhost/Myapp/course.php";

        handler.open();
        Cursor c = handler.returnData();
        if (c.getCount() == 0) {
            Toast.makeText(context, "No Data Found", Toast.LENGTH_LONG).show();
        }

        obj = new JSONObject();
        while (c.moveToNext()) {
            String cid = c.getString(0);
            String name = c.getString(1);
            obj.put("cid", Integer.parseInt(cid));
            obj.put("cname", name);
        }
        handler.close();
        array = new JSONArray();
        array.put(obj);

        sendObj = new JSONObject();
        sendObj.put("course", array);

        String data = sendObj.toString();

        URL url = new URL(link);
        URLConnection conn = url.openConnection();
        conn.setDoInput (true);
        conn.setDoOutput (true);
        conn.setUseCaches (false);
        conn.setRequestProperty("Content-Type","application/json");
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

        wr.write(data);
        wr.flush();

        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        StringBuilder sb = new StringBuilder();
        String line = null;

        // Read Server Response
        while ((line = reader.readLine()) != null) {
            sb.append(line);
            break;
        }
        return sb.toString();
    } catch (MalformedURLException e) {
    } catch (UnsupportedEncodingException e) {
    } catch (IOException e) {
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();


    }
    return null;
}    

【讨论】:

  • 这一行在android studio wr.write(data.getBytes())上显示错误;
  • 很抱歉,我忽略了这里使用的是阅读器而不是流式传输。所以删除 getBytes() 部分。立即检查。
猜你喜欢
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
  • 1970-01-01
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多