php - Android JSON 到 PHP 服务器

标签 php android mysql json

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

Android 代码

   @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());  
?>

最佳答案

您需要设置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;
}    

关于php - Android JSON 到 PHP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35086236/

相关文章:

php - mysql 连接到常量并集

mysql - 使用 Node.js 搜索 PDF 文本

php - 无法获取 PHP 变量的 MySQL 表条目

mysql - 不仅匹配一个单词,还匹配整个表格

php - 我只需要从另一个 html/php 页面导入表格

php - 需要在 php 中添加一个计数器以每 4 列分隔行

javascript - 从 AJAX 获取多张图片,但每张图片都在图片扩展后显示

android - 从非 Activity 中完成 Activity

javascript - Apache Cordova 联系方式查找

android - RUBYMOTION_ANDROID_SDK 不正确