我目前正在使用一个登录users.Upon登录的Android应用程序,允许用户自己创建一个文件名。但是,执行此函数时,如下所示,返回json包含登录详细信息的JSON字符串。
我正在使用xmapp服务器。
有没有人知道获取createNewFile详细信息的返回json字符串的正确方法?
public JSONObject createNewFile(String f_id, String userid, String name){
// Building Parameters
List params= new ArrayList();
params.add(new BasicNameValuePair("tag", createFile));
params.add(new BasicNameValuePair("fid", fid));
params.add(new BasicNameValuePair("userid", userid));
params.add(new BasicNameValuePair("name", name));
// getting JSON Object
JSONObject json = jsonParser.getJSONFromUrl(URL, params);
// return json
return json;
}
//用于登录
public JSONObject loginUsers(String email, String password){
// Building Parameters
List params = new ArrayList();
params.add(new BasicNameValuePair("tag", login_tag));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
// return json
Log.e("JSON", json.toString());
return json;
}
这是我的JSONPARSER
课程。
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url, List params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// 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;
}
}
Php文件:
DB_Functions
public function storeFile($fid, $userid, $name){
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysqli_select_db($con, DB_DATABASE);
$result = mysqli_query($con,"INSERT INTO file_info(fid, userid, name) VALUES('$fid', '$userid', '$name', NOW())");
// check for successful store
if ($result) {
// get file details
$fid = mysqli_insert_id($con); // last inserted id
$result = mysqli_query($con,"SELECT * FROM file_info WHERE fid = $fid");
// return file details
return mysqli_fetch_array($result);
} else {
return false;
}
}
的index.php
//add_file
if ($tag == 'add_file') {
// Request type is add new file
$fid = $_POST['fid'];
$userid = $_POST['userid'];
$name = $_POST['name'];
$file_info= $db->storeFile($fid, $userid, $name);
if ($file_info) {
// user stored successfully
$response["success"] = 1;
$response["fid"] = $file_info["fid"];
$response["file_info"]["userid"] = $file_info["userid"];
$response["file_info"]["name"] = $ufile_info["name"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = 1;
$response["error_msg"] = "Error occured in Adding";
echo json_encode($response);
}
}else {
echo "Invalid Request";
}
} else {
echo "Access Denied";
}
面临错误:
Invalid RequestInvalid Requestn
Error parsing data org.json.JSONException: Value Invalid of type java.lang.String cannot be converted to JSONObject