Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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 安卓:获得工作,发布不会_Android_Json_Post_Get - Fatal编程技术网

Android 安卓:获得工作,发布不会

Android 安卓:获得工作,发布不会,android,json,post,get,Android,Json,Post,Get,我目前正在测试一些用于android编程的代码。我在这里找到了一些测试项目: 但问题是,我可以毫无问题地读取数据库,但函数不适用于POST only GET。因此,当我尝试添加新产品时,它会向我的php脚本发出请求,但根本没有POST数据。我是这样测试的: <?php header("content-type:application/json; charset=UTF-8"); //print("fda"); /* * Following code will create a new

我目前正在测试一些用于android编程的代码。我在这里找到了一些测试项目:

但问题是,我可以毫无问题地读取数据库,但函数不适用于POST only GET。因此,当我尝试添加新产品时,它会向我的php脚本发出请求,但根本没有POST数据。我是这样测试的:

<?php 
header("content-type:application/json; charset=UTF-8");
//print("fda");
/*
 * Following code will create a new product row
 * All product details are read from HTTP Post Request
 */

// array for JSON response
$response = array();

// check for required fields
//if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {
//if (isset($_POST['name'])){
    $name = "test";//$_POST['name'];
    $price = 123; //$_POST['price'];
    $description = "desc"; //$_POST['description'];

foreach ($_POST as $key => $value)
 $data = $data." Field ".htmlspecialchars($key)." is ".htmlspecialchars($value);
    // include db connect class
//echo $data;
$url = $_SERVER['REQUEST_URI'];
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO products(name, price, description, url) VALUES('$name', '$price', '$description','$data')");

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Product successfully created!.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

        // echoing JSON response
        echo json_encode($response);

    }
//} else {
//    // required field is missing
//    $response["success"] = 0;
//    $response["message"] = "Required field(s) is missing";
//
//    // echoing JSON response
//    echo json_encode($response);
//}
?>

Hi User i使用以下代码调用post到web服务。它可能会帮助你

**public String doPost(Activity activity, String urlString, String method, String value) throws ClientProtocolException, IOException { String responseString=""; HttpURLConnection urlConnection = null; retryCount++; try { URL url = new URL(urlString+method); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("POST"); urlConnection.setRequestProperty("Content-Type", "application/json"); urlConnection.setRequestProperty("Content-Length",value.length()+"" ); urlConnection.setDoInput( true ); urlConnection.setDoOutput( true ); DataOutputStream dos = new DataOutputStream( urlConnection.getOutputStream() ); byte[] bs = value.getBytes(); Log.d(tag, "Sending JSON String ->"+new String(bs)); dos.write(bs); dos.flush(); dos.close(); Log.d(tag,"Responce ->"+urlConnection.getResponseMessage()); if(urlConnection.getResponseMessage().toLowerCase().equals("ok")) { InputStream is = urlConnection.getInputStream(); int ch; StringBuffer b =new StringBuffer(); while( ( ch = is.read() ) != -1 ) { b.append( (char)ch ); } responseString = b.toString(); Log.d(tag,method + "','" + responseString); return method + "','" + responseString; } else { Log.d(tag, tag1+urlConnection.getResponseMessage()); } dos.close(); } catch (SocketException e) { Log.d(tag, tag1+e); } catch (Exception e) { Log.e(tag,"-->"+ e); } return ""; }**
在这里,URL字符串和方法构成了web服务方法的整个URL。

Hi,谢谢你叫我用户,我看到我没有名字。但回到话题上来,我试过了,但实际上并没有成功。这一切似乎都没有发送任何POST消息。也许需要在某处导入一些额外的文件?可能是发送错误的句子?嗨,liquidacid,很抱歉打电话给你的用户bcoz,我正在处理一个名为user的类。我在里面贴了一个json标签。不,您不需要任何外部文件或jar来进行此调用。 **public String doPost(Activity activity, String urlString, String method, String value) throws ClientProtocolException, IOException { String responseString=""; HttpURLConnection urlConnection = null; retryCount++; try { URL url = new URL(urlString+method); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("POST"); urlConnection.setRequestProperty("Content-Type", "application/json"); urlConnection.setRequestProperty("Content-Length",value.length()+"" ); urlConnection.setDoInput( true ); urlConnection.setDoOutput( true ); DataOutputStream dos = new DataOutputStream( urlConnection.getOutputStream() ); byte[] bs = value.getBytes(); Log.d(tag, "Sending JSON String ->"+new String(bs)); dos.write(bs); dos.flush(); dos.close(); Log.d(tag,"Responce ->"+urlConnection.getResponseMessage()); if(urlConnection.getResponseMessage().toLowerCase().equals("ok")) { InputStream is = urlConnection.getInputStream(); int ch; StringBuffer b =new StringBuffer(); while( ( ch = is.read() ) != -1 ) { b.append( (char)ch ); } responseString = b.toString(); Log.d(tag,method + "','" + responseString); return method + "','" + responseString; } else { Log.d(tag, tag1+urlConnection.getResponseMessage()); } dos.close(); } catch (SocketException e) { Log.d(tag, tag1+e); } catch (Exception e) { Log.e(tag,"-->"+ e); } return ""; }**