我将为Android开发一个Web应用程序但是我第一次尝试这个实验,所以我无法完成我的任务,因为我是新手。它给我一个像字符串的错误无法转换为JSONObject。请帮助我,我已经尝试了一切,但还没有得到任何解决方案。这是我的代码。
<?php
$host='127.0.0.1';
$uname='root';
$pwd='password';
$db="resumemaker";
$con = mysql_connect("localhost", "root");
mysql_select_db($db,$con) or die("db selection failed");
$fname=$_REQUEST['fname'];
$email=$_REQUEST['email'];
$mobile=$_REQUEST['mobile'];
$passwd=$_REQUEST['passwd'];
$flag['code']=0;
if($r=mysql_query("insert into sample values('$fname','$email','$mobile','$passwd') ",$con))
{
$flag['code']=1;
//echo"hi";
}
print(json_encode($flag));
mysql_close($con);
?>
这是我的PHP代码
ObjectId
提前致谢....
答案 0 :(得分:0)
您没有将其编码为json,请参阅此示例将帮助您完成100%的工作代码
的index.php
<?php
header("Content-Type:application/json");
include("function.php");
$fname=$_REQUEST['fname'];
$email=$_REQUEST['email'];
$mobile=$_REQUEST['mobile'];
$passwd=$_REQUEST['passwd'];
$record = fetchrecord($fname,$email,$mobile,$passwd);
if(empty($record))
{
deliverresponse(200,"Record not found",NULL,0);
}
else
{
deliverresponse(200,"Record Displayed",$record,1);
}
function deliverresponse($status,$status_message,$data,$success)
{
header("HTTP/1.1 $status $status_message");
$response['status'] = $status;
$response['status_message'] = $status_message;
$response['code'] = $code;
$response['data'] = $data;
$jsonresponse = json_encode($response);
echo $jsonresponse;
}
?>
Function.php
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db('your db name', $conn);
function fetchrecord($fname,$email,$mobile,$passwd)
{
$qur = mysql_query("INSERT INTO `sample` (fname,email,mobile,passwd) VALUES (NULL, '$fname','$email','$mobile','$passwd')") or die ("Query Failed");
if($qur)
{
$msg = "Record Inserted";
$user[] = array($msg);
}
else
{
$msg = "Record not Inserted";
$user[] = array($msg);
}
return $user;
}
使用Async Class进行网络线程
public class SignupProcess extends AsyncTask<String, String, String>
{
JSONObject json = new JSONObject();
private static final String TAG_SUCCESS = "code";
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(signup.this);
pDialog.setMessage("Login..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... args) {
List<NameValuePair> pair = new ArrayList<>();
pair.add(new BasicNameValuePair("name", name));
pair.add(new BasicNameValuePair("email", email));
pair.add(new BasicNameValuePair("password", mobile));
pair.add(new BasicNameValuePair("status",String,psswd);
json = JSONParser.makeHttpRequest("http://xxxxxxx","GET", pair);
Log.d("Create Response", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
getstatus = success;
if (success == 1) {
Log.d("success!", json.toString());
}
else if (success==0){
return json.getString(TAG_SUCCESS);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
JSONParser.java
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public static String Result=null;
public JSONParser()
{
}
public static JSONObject makeHttpRequest(String url,String method, List<NameValuePair> pair) {
try {
// check for request method
if(method.equals("POST")){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(pair));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method.equals("GET")){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(pair, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
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, HTTP.UTF_8), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Result = 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 jObj;
}
}
按钮单击侦听器写入
new SignupProcess().execute();