我的应用尝试在服务器中创建新行。
我得到的错误是jObj = new JSONObject(json);
这是创建新行的php文件:
<?php
$response = array();
if (isset($_POST['user']) && isset($_POST['pass']) & isset($_POST['mail'])&& isset($_POST['num']))
{
$user = $_POST['user'];
$pass = $_POST['pass'];
$mail = $_POST['mail'];
$num = $_POST['num'];
require_once __DIR__ . '/db_connect2.php';
$db = new DB_CONNECT();
$result = $db->query("INSERT INTO users(Name, Password, Email,ConfirmNum) VALUES('$user', '$pass', '$mail', '$num')");
if ($result) {
$response["success"] = 1;
$response["message"] = "user successfully created.";
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
echo json_encode($response);
}
} else {
$response["success"] = 0;
$response["message"] = "missing fields";
echo json_encode($response);
}
?>
请求更新的解析器是:
package com.example.denis.onthego;
import android.content.ContentValues;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class JSONParser {
static JSONObject jObj;
static String json;
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public static JSONObject makeHttpRequest(String url, String method, ContentValues params) {
// Making HTTP request
try {
final OkHttpClient client = new OkHttpClient();
Request request;
// check for request method
if (method.equals("POST")) {
// request method is POST
MediaType contentType = MediaType.parse("application/x-www-form-urlencoded; charset=UTF-8");
String content = "";
for (String key : params.keySet())
{
if ( !content.isEmpty())
content += "&";
content += key + "=" + params.get(key);
}
RequestBody body = RequestBody.create(contentType, content);
request = new Request.Builder().url(url).post(body).build();
}
else {
// request method is GET
request = new Request.Builder().url(url).build();
}
final Response response = client.newCall(request).execute();
json = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e ){
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
对解析器的调用是:
JSONObject json = jsonParser.makeHttpRequest(url_create_product,"POST", params);
虽然“add_user”用于创建新行的php文件(它是正确的URL),而“params”不是空的并且包含正确的键和内容。 解析器或php文件有问题吗? 这是一个非常严肃的学校项目,这是我唯一缺少的。
以下是参数:
params.put("user", "swane15");
params.put("pass", "asdeg124A");
params.put("mail", "asf@asd.com");
params.put("num", "111111");
为什么php文件没有返回任何内容?是因为应用程序无法访问它或者php文件本身是否有错误?
答案 0 :(得分:0)
MediaType JSON = MediaType.parse("Content-Type:application/json; charset=UTF-8");
RequestBody body = RequestBody.create(JSON, params.toString());
更改为:
MediaType contentType = MediaType.parse("application/x-www-form-urlencoded; charset=UTF-8");
String content = "";
for (String key : params.keySet())
{
if ( !content.isEmpty())
content += "&";
content += key + "=" + params.get(key);
}
RequestBody body = RequestBody.create(contentType, content);
要做到正确,你应该URLEncoder.encode()值。