Android 分析数据org.json.JSONException时出错:Value;无法将java.lang.String类型的转换为JSONObject
我正在尝试通过servlet使用Json对从android studio到mySQL的应用程序进行身份验证。 我一直收到这个解析错误。。下面是我的代码片段。请帮帮我 由android studio编写的MainActivity类Android 分析数据org.json.JSONException时出错:Value;无法将java.lang.String类型的转换为JSONObject,android,mysql,servlets,Android,Mysql,Servlets,我正在尝试通过servlet使用Json对从android studio到mySQL的应用程序进行身份验证。 我一直收到这个解析错误。。下面是我的代码片段。请帮帮我 由android studio编写的MainActivity类 -------------- public class MainActivity extends ActionBarActivity { EditText uname, password; Button submit; JSONParser j
--------------
public class MainActivity extends ActionBarActivity {
EditText uname, password;
Button submit;
JSONParser jParser = new JSONParser();
JSONObject json;
private static String url_login = "http://192.168.10.125:8080/CabbCallLogin/LoginServlet";
//JSONArray incoming_msg = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewsById();
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View args0) {
// execute method invokes doInBackground() where we open a Http URL connection using the given Servlet URL
//and get output response from InputStream and return it.
new Login().execute();
}
});
}
private void findViewsById() {
uname = (EditText) findViewById(R.id.txtUser);
password = (EditText) findViewById(R.id.txtPass);
submit = (Button) findViewById(R.id.button1);
}
private class Login extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... args) {
Log.e("Error001" , "I am in background");
// Getting username and password from user input
String username = uname.getText().toString();
String pass = password.getText().toString();
Log.e("Error004", "I have got the uname,pass");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("u",username));
params.add(new BasicNameValuePair("p",pass));
Log.e("Error00", "I have got the name value pairs");
json = jParser.makeHttpRequest(url_login, "GET", params);
Log.e("Error00" , "I have sent the request");
String s=null;
try{
s= json.getString("info");
Log.e("Msg", json.getString("info"));
Log.e("Error" , "I am in the try catch");
if(s.equals("success")){
Log.e("Error1" , "I am in the if try catch");
Intent login = new Intent(getApplicationContext(), Welcome.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
finish();
}else{
Log.e("Error2" , "I am in the else try catch");
ErrorMessage("Error" , "I m in the else part of login");
}
}catch (JSONException e){
Log.e("Error3" , "I am in the json try catch");
e.printStackTrace();
ErrorMessage("Error", "I m in the json exception part of login");
}
return null;
}
}
private void ErrorMessage(String Title , String Message){
AlertDialog.Builder alertbuilder = new AlertDialog.Builder(MainActivity.this);
alertbuilder.setMessage("Incorrect Credentials");
alertbuilder.setPositiveButton("Ok", null);
alertbuilder.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
------------------------------------------
JSONParser class
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
static InputStream iStream = null;
static JSONArray jarray = null;
public JSONParser() {
}
public JSONObject makeHttpRequest(String url_login, String method,
List<NameValuePair> params) {
// Making HTTP request
Log.e("Error003" , "I am in the JSONOBJECT class");
try {
if(method == "POST"){
// request method is POST
// defaultHttpClient
Log.e("Error003" , "I am in the JSONOBJECT post method");
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_login);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
Log.e("Error003" , "I am in the JSONOBJECT get method");
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url_login += "?" + paramString;
HttpGet httpGet = new HttpGet(url_login);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
Log.e("Error004" , "I am at end the JSONOBJECT get method");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try{
Log.e("Error003" , "I am in the Buffer class");
BufferedReader reader = new BufferedReader(new InputStreamReader (is, "UTF-8"), 8);
Log.e("Error004" , "I am in the Buffer class");
StringBuilder sb = new StringBuilder();
Log.e("Error005" , "I am in the Buffer class");
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
}catch (Exception e) {
Log.e("Buffer Error1", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser1", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
public JSONArray getJSONFromUrl(String url_login,String method, List<NameValuePair> params) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url_login);
try{
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e("==>", "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
jarray = new JSONArray( builder.toString());
} catch (JSONException e) {
Log.e("JSON Parser2", "Error parsing data " + e.toString());
}
// return JSON Object
return jarray;
}
public JSONObject makeHttpRequest2(String url_login) {
// Making HTTP request
try {
// check for request method
//if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_login);
// 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, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error2", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser3", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
--------------
公共类MainActivity扩展了ActionBarActivity{
EditText uname,密码;
按钮提交;
JSONParser jParser=新的JSONParser();
JSONObject json;
私有静态字符串url\u登录=”http://192.168.10.125:8080/CabbCallLogin/LoginServlet";
//JSONArray传入消息=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewsById();
submit.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图args0){
//execute方法调用doInBackground(),在这里我们使用给定的Servlet URL打开Http URL连接
//从InputStream获取输出响应并返回它。
新登录().execute();
}
});
}
私有void findViewsById(){
uname=(EditText)findViewById(R.id.txtUser);
密码=(EditText)findViewById(R.id.txtPass);
提交=(按钮)findViewById(R.id.button1);
}
私有类登录扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…args){
Log.e(“Error001”,“我在后台”);
//从用户输入中获取用户名和密码
字符串username=uname.getText().toString();
字符串pass=password.getText().toString();
Log.e(“Error004”,“我得到了uname,pass”);
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“u”,用户名));
参数添加(新的BasicNameValuePair(“p”,通过));
Log.e(“Error00”,“我得到了名称-值对”);
json=jParser.makeHttpRequest(url_login,“GET”,params);
Log.e(“Error00”,“我已发送请求”);
字符串s=null;
试一试{
s=json.getString(“info”);
Log.e(“Msg”,json.getString(“info”);
Log.e(“错误”,“我正在尝试捕捉”);
如果(s.equals(“成功”)){
Log.e(“Error1”,“我在if-try-catch中”);
Intent login=newintent(getApplicationContext(),Welcome.class);
login.addFlags(Intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
startActivity(登录);
完成();
}否则{
Log.e(“Error2”,“我在其他尝试捕捉中”);
ErrorMessage(“错误”,“我在登录的其他部分”);
}
}捕获(JSONException e){
Log.e(“Error3”,“我在json try catch中”);
e、 printStackTrace();
ErrorMessage(“错误”,“我在登录的json异常部分”);
}
返回null;
}
}
私有无效错误消息(字符串标题、字符串消息){
AlertDialog.Builder alertbuilder=新建AlertDialog.Builder(MainActivity.this);
alertbuilder.setMessage(“不正确的凭据”);
alertbuilder.setPositiveButton(“确定”,null);
alertbuilder.show();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}
------------------------------------------
JSONParser类
公共类JSONParser{
静态InputStream为空;
静态JSONObject jObj=null;
静态字符串json=“”;
静态InputStream iStream=null;
静态JSONArray jarray=null;
公共JSONParser(){
}
公共JSONObject makeHttpRequest(字符串url\u登录,字符串方法,
列表参数){
//发出HTTP请求
Log.e(“Error003”,“我在JSONOBJECT类中”);
试一试{
如果(方法==“POST”){
//请求方法为POST
//defaultHttpClient
Log.e(“Error003”,“我在JSONOBJECT post方法中”);
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url\u登录);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}else if(方法==“GET”){
//请求方法是GET
e(“Error003”,“我在JSONOBJECT get方法中”);
DefaultHttpClient httpClient=新的DefaultHttpClient();
String paramString=URLEncodedUtils.format(params,“utf-8”);
url_login+=“?”+参数字符串;
HttpGet HttpGet=新的HttpGet(url\u登录);
HttpResponse HttpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
Log.e