org.json.JSONException:Value <script of =“”type =“”java.lang.string =“”can =“”be =“”converted =“”to =“”jsonobject =“”

时间:2016-06-03 09:51:26

标签: java php android json

=“”

我正在尝试学习如何为我的Android应用程序创建登录和注册系统。我关注了YouTube视频,并在运行应用程序时遇到此错误。

&#xA;&#xA;

错误日志:

&#xA;&#xA; < pre> 06-03 14:32:14.646 2563-2563 / example.login W / System.err:org.json.JSONException:Value&lt; html&gt;&lt; body&gt;&lt; java.lang类型的脚本。字符串无法转换为JSONObject&#xA; 06-03 14:32:14.646 2563-2563 / example.login W / System.err:at org.json.JSON.typeMismatch(JSON.java:111)&#xA; 06 -03 14:32:14.646 2563-2563 / example.login W / System.err:at org.json.JSONObject。&lt; init&gt;(JSONObject.java:160)&#xA; 06-03 14:32:14.646 2563-2563 / example.login W / System.err:at org.json.JSONObject。&lt; init&gt;(JSONObject.java:173)&#xA; 06-03 14:32:14.646 2563-2563 / example.login W / System.err:at vipanchithreddy.login.RegisterActivity $ 1 $ 1.onResponse(RegisterActivity.java:41)&#xA; 06-03 14:32:14.646 2563-2563 / example.login W / System.err:at vipanchithreddy .login.RegisterActivity $ 1 $ 1.onResponse(RegisterActivity.java:37)&#xA; 06-03 14:32:14.647 2563-2563 / example.login W / System.err:at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)&#xA; 06-03 14:32:14.647 2563-2563 / example.login W / System .err:at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)&#xA; 06-03 14:32:14.647 2563-2563 / example.login W / System.err:at com。 android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)&#xA; 06-03 14:32:14.647 2563-2563 / example.login W / System.err:at android.os.Handler.handleCallback( Handler.java:739) 06-03 14:32:14.647 2563-2563 / example.login W / System.err:at android.os.Handler.dispatchMessage(Handler.java:95)&#xA; 06-03 14:32:14.647 2563-2563 / example.login W / System.err:在android.os.Looper.loop(Looper.java:148)&#xA; 06-03 14:32:14.647 2563- 2563 / example.login W / System.err:在android.app.ActivityThread.main(ActivityThread.java:5417)&#xA; 06-03 14:32:14.647 2563-2563 / example.login W / System.err :at java.lang.reflect.Method.invoke(Nati ve方法)&#xA; 06-03 14:32:14.647 2563-2563 / example.login W / System.err:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)& #xA; 06-03 14:32:14.647 2563-2563 / example.login W / System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)&#xA; &#xA;&#xA;

我是JSON编码的新手。我读了很多关于这个问题的线索,但仍然不知道如何解决它。这些是我的php和java文件。

&#xA;&#xA;

Register.php:

&#xA;&#xA;
 &lt;?php&# XA; $ con = mysqli_connect(“my_server”,“my_user”,“my_password”,“my_database”);&#xA;&#xA; $ username = $ _POST [“username”];&#xA; $ password = $ _POST [“password”];&#xA; $ statement = mysqli_prepare($ con,“INSERT INTO user(username,password)VALUES(?,?)”);&#xA; mysqli_stmt_bind_param($ statement,“ss”,$ username,$ password);&#xA; mysqli_stmt_execute($语句);&#XA;&#XA; $ response = array();&#xA; $ response [“success”] = true;&#xA;&#xA; echo json_encode($ response);&#xA;?&gt;&#xA;  
&#xA;&#xA;

RegisterRequest.java:

&#xA;& #xA;
  package example.login;&#xA;&#xA; import com.android.volley.Response;&#xA; import com.android.volley.toolbox.StringRequest;&#xA; &#xA; import java.util.HashMap;&#xA; import java.util.Map;&#xA;&#xA; public class RegisterRequest extends StringRequest {&#xA; private static final String REGISTER_REQUEST_URL =“http://www.vipanchith.byethost17.com/file/Register.php";
私有地图&lt; String,String&gt; PARAMS;&#XA;&#XA; public RegisterRequest(String username,String password,Response.Listener&lt; String&gt; listener){&#xA; super(Method.POST,REGISTER_REQUEST_URL,listener,null);&#xA; params = new HashMap&lt;&gt;();&#xA; params.put(“username”,username);&#xA; params.put(“密码”,密码);&#xA; }&#XA;&#XA; @越权#XA;公共地图&lt; String,String&gt; getParams(){&#xA;返回参数;&#xA; }&#XA;}&#XA;  
&#XA;&#XA;

RegisterActivity.java:

&#XA;&#XA;
 <代码> package example.login;&#xA;&#xA; import android.content.Intent;&#xA; import android.os.Bundle;&#xA; import android.support.v7.app.AlertDialog;&#xA ; import android.support.v7.app.AppCompatActivity;&#xA; import android.view.View;&#xA; import android.widget.Button;&#xA; import android.widget.EditText;&#xA;& #xA; import com.android.volley.RequestQueue;&#xA; import com.android.volley.Response;&#xA; import com.android.volley.toolbox.Volley;&#xA;&#xA; import org .json.JSONException;&#xA; import org.json.JSONObject;&#xA;&#xA; public class RegisterActivity扩展AppCompatActivity {&#xA;&#xA; @越权#XA; protected void onCreate(Bundle savedInstanceState){&#xA; super.onCreate(savedInstanceState);&#XA;的setContentView(R.layout.activity_register);&#XA;&#XA; final EditText etUsername =(EditText)findViewById(R.id.etUsername);&#xA; final EditText etPassword =(EditText)findViewById(R.id.etPassword);&#xA;&#xA; final Button bRegister =(Button)findViewById(R.id.bRegister);&#xA;&#xA;断言bRegister!= null;&#xA; bRegister.setOnClickListener(new View.OnClickListener(){&#xA; @Override&#xA; public void onClick(View v){&#xA; final String username = etUsername.getText()。toString();&#xA; final String password = etPassword.getText()。toString();&#xA;&#xA; Response.Listener&lt; String&gt; responseListener = new Response.Listener&lt; String&gt;(){&#xA; @Override&#xA; public void onResponse(String response){&#xA; try {&#xA; JSONObject jsonResponse = new JSONObject(response);&#xA; boolean success = jsonResponse.getBoolean(“success”);&#xA;&#xA; if(success){&#xA; Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);&#xA; RegisterActivity.this.startActivity(intent);&#xA;} else {&#xA; AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);&#xA; builder.setMessage(“注册失败”)&#xA; .setNegativeButton(“Retry”,null)&#xA; .create()&#XA; .show();&#XA; }&#XA; } catch(JSONException e){&#xA; e.printStackTrace();&#XA; }&#XA; }&#XA; };&#XA;&#XA; RegisterRequest registerRequest = new RegisterRequest(username,password,responseListener);&#xA; RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);&#xA; queue.add(registerRequest);&#XA; }&#XA; });&#XA; }&#xA;}&#xA;  
&#xA;&#xA;

任何帮助将不胜感激。谢谢。

&#xA;

2 个答案:

答案 0 :(得分:2)

谢谢大家,代码没问题。这是一个额外的保护,由byethost.com故意添加,以阻止机器人访问我们的网站,从而为我们的脚本带来巨大的安全优势。我将这些文件重新上传到000webhost.com,它就像一个魅力。 有关详细信息,请参阅此讨论 ByetHost server passing html values "Checking your browser" with JSON String

答案 1 :(得分:0)

如果json有值,则会出现此错误。

{
   test:[
    {
       id:1,
       name:me
   },
  {
   id:2,
   name:you
 }
]
}

如果json没有价值......

{

}

...然后发生了错误。