Java 分析数据org.json.JSONException时出错:在的字符0处输入结束
以下是我的java代码: 类UpdateProfile扩展了AsyncTask{Java 分析数据org.json.JSONException时出错:在的字符0处输入结束,java,php,android,Java,Php,Android,以下是我的java代码: 类UpdateProfile扩展了AsyncTask{ /** * Before starting background thread Show Progress Dialog */ @Override protected void onPreExecute() { super.onPreExecute(); Log.d("UpdateProfile", "pre-execute");
/**
* Before starting background thread Show Progress Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.d("UpdateProfile", "pre-execute");
}
/**
* Creating product
*/
protected String doInBackground(String... args) {
String status = null;
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", "kjljkl"));
params.add(new BasicNameValuePair("imageuri","lkjlk" ));
params.add(new BasicNameValuePair("username","klkjljl" ));
params.add(new BasicNameValuePair("bio","lklklkl" ));
// getting JSON Object
// Note that create product url accepts POST method
Log.d("UpdateProfile", "Before-HttpRequest");
JSONObject json = jsonParser.makeHttpRequest(url_update_profile, "POST", params);
// check log cat fro response
Log.d("UpdateProfile", "After-HttpRequest");
// check for success tag
try {
success = json.getInt("success");
if (success == 1) {
// successfully created product
Log.d("UpdateProfile", "user profile updated");
status = "success";
// closing this screen
// finish();
} else {
// failed to create product
Log.d("UpdateProfile", "user profile not updated");
status="fail";
}
} catch (JSONException e) {
e.printStackTrace();
}
return status;
}
/**
* After completing background task Dismiss the progress dialog
* *
*/
protected void onPostExecute(String status) {
// dismiss the dialog once done
if(status.equals("success"))
{
Log.d("UpdateProfile", "user profile updated");
//Toast.makeText(getApplicationContext(), "Login success", Toast.LENGTH_LONG).show();
//Intent intent = new Intent(getApplicationContext(), Comment.class);
//startActivity(intent);
}
else
{
Log.d("UpdateProfile", "user profile not updated");
//Toast.makeText(getApplicationContext(), "Login error", Toast.LENGTH_LONG).show();
}
}
}
/**
*在启动后台线程显示进度对话框之前
*/
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
Log.d(“更新配置文件”、“预执行”);
}
/**
*创造产品
*/
受保护的字符串doInBackground(字符串…args){
字符串状态=空;
//建筑参数
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“电子邮件”、“kjljkl”);
添加(新的BasicNameValuePair(“imageuri”、“lkjlk”);
参数添加(新的BasicNameValuePair(“用户名”、“KLKJL”);
参数添加(新的BasicNameValuePair(“bio”、“LKL”);
//获取JSON对象
//请注意,创建产品url接受POST方法
Log.d(“UpdateProfile”,“在HttpRequest之前”);
JSONObject json=jsonParser.makeHttpRequest(url_update_profile,“POST”,params);
//检查cat fro响应日志
Log.d(“UpdateProfile”,“HttpRequest之后”);
//检查成功标签
试一试{
success=json.getInt(“success”);
如果(成功==1){
//已成功创建产品
Log.d(“更新配置文件”,“用户配置文件更新”);
status=“success”;
//关闭此屏幕
//完成();
}否则{
//未能创建产品
Log.d(“UpdateProfile”,“用户配置文件未更新”);
status=“fail”;
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回状态;
}
/**
*完成后台任务后,关闭“进度”对话框
* *
*/
受保护的void onPostExecute(字符串状态){
//完成后关闭对话框
如果(状态等于(“成功”))
{
Log.d(“更新配置文件”,“用户配置文件更新”);
//Toast.makeText(getApplicationContext(),“登录成功”,Toast.LENGTH_LONG.show();
//Intent Intent=新的Intent(getApplicationContext(),Comment.class);
//星触觉(意向);
}
其他的
{
Log.d(“UpdateProfile”,“用户配置文件未更新”);
//Toast.makeText(getApplicationContext(),“登录错误”,Toast.LENGTH_LONG.show();
}
}
}
以下是我的Php代码:
<?php
if (isset($_POST['email'])&& isset($_POST['imageuri'])&& isset($_POST['username'])&& isset($_POST['bio'])){
// In my previous post for question, i did not give the code as I thought it will not hit this if statement. But as it confused all, I am giving the code snippet here
if($imageuri != null)
{
// Insert into Db
$response["success"] = 1;
$response["message"] = "UserProfile successfully inserted in imageuri.";
echo json_encode($response);
}
// Do the same for other 2 params - username, bio, as well
}
?>
我的代码中出现了什么错误,出现了以下错误?
分析数据org.json.JSONException时出错:在的字符0处输入结束
请帮助我。
设置POST变量时,您似乎没有输出任何json。如果这是一个学习练习,我可以理解尝试手动生成请求。如果不是,那么有更好的工具专门针对这个场景,比如If语句在php上没有任何作用。它不返回json,那么它如何解析不存在的json呢?我根据请求添加了if语句代码。Brandon的回答告诉我,当我们不发送json响应时会出现此错误。我在发送响应时,imageuri、username、bio不等于null。但是,我没有对各自的else条件发送任何响应。一旦我添加了带有响应的else语句,这个错误就消失了。@Davidpostill,是的,这个答案确实回答了问题。“尝试在这里重复一些json”。对不必要的反对票感到高兴
<?php
if (isset($_POST['email'])&& isset($_POST['imageuri'])&& isset($_POST['username'])&& isset($_POST['bio'])){
}