如何制作一个;生的;Java中JSON的字符串

如何制作一个;生的;Java中JSON的字符串,java,php,android,mysql,json,Java,Php,Android,Mysql,Json,我正在为学校atm机编写一个android应用程序。我很忙,想请你们帮帮我。 到目前为止,我了解了如何使用PHP打印MySQL数据库中的数据,以便在Java中进行解析。然后我的代码(取自此处:数据并将其转换为我相信的字符串) 所以我的问题是:我如何为eksample和if语句检查用户数据是否正确?我就是不明白 以下是我使用的活动(登录表单)的所有代码: package com.example.com; 导入java.io.BufferedReader; 导入java.io.InputStream

我正在为学校atm机编写一个android应用程序。我很忙,想请你们帮帮我。 到目前为止,我了解了如何使用PHP打印MySQL数据库中的数据,以便在Java中进行解析。然后我的代码(取自此处:数据并将其转换为我相信的字符串)

所以我的问题是:我如何为eksample和if语句检查用户数据是否正确?我就是不明白

以下是我使用的活动(登录表单)的所有代码:

package com.example.com;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.ActionBar;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
公共类MainActivity扩展了活动{
//Intent i=新的Intent(getBaseContext(),MainPageActivity.class);
//星触觉(i);
按钮登录;
编辑文本用户名;
编辑文本密码;
文本视图lol;
杰索纳雷·贾雷;
字符串结果=null;
InputStream=null;
StringBuilder sb=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ActionBar ActionBar=getActionBar();
//actionBar.hide();
lol=(TextView)findViewById(R.id.textView1);
login=(按钮)findviewbyd(R.id.loginbutton);
username=(EditText)findViewById(R.id.usernametext);
密码=(EditText)findViewById(R.id.passwordtext);
login.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
字符串usernameget=username.getText().toString();
字符串passwordget=password.getText().toString();
if(usernameget.contentEquals(“”){
username.setText(“需要usernam”);
}否则{
if(passwordget.contentEquals(“”){
username.setText(“需要密码”);
}否则{
字符串userid=username.getText().toString();
Intent Intent=newintent(getBaseContext(),Side.class);
intent.putExtra(“sessionid”,userid);
星触觉(意向);
}
}
}
});
字符串结果=”;
//要发送的年度数据
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(结果,结果));
//http post
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://http://fungi.webatu.com/Index.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
InputStream=entity.getContent();
}捕获(异常e11){
Log.e(“Log_标记”,“Fejl i HTTP连接”+e11.toString());
}
//将响应转换为字符串
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
Log.e(“Log_标记”,“Fejl i resultat konvertering”+e.toString());
}
//解析json数据
试一试{
JSONArray jArray=新JSONArray(结果);

对于(inti=0;i我不确定您的问题,但我认为您希望从Web服务获取一些JSON数据

开发到Android,您不能在主线程上使用网络资源。因此,您需要启动一个新线程。要实现这一点,您可以使用AsyncTask或使用thread类

    public static JSONArray getJSONArrayFromURL(String url) {

    // initialize
    InputStream is = null;
    String result = "";
    JSONArray jArray = null;

    Log.d("URL_SEARCH", url);

    // http post
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }

    // convert response to string
    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();
        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jArray = new JSONArray(result);

        Log.d("URL_SEARCH", "JSON: " + jArray.length());

    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }

    return jArray;
}

我不确定您的问题,但我认为您希望从Web服务获取一些JSON数据

开发到Android,您不能在主线程上使用网络资源。因此,您需要启动一个新线程。要实现这一点,您可以使用AsyncTask或使用thread类

    public static JSONArray getJSONArrayFromURL(String url) {

    // initialize
    InputStream is = null;
    String result = "";
    JSONArray jArray = null;

    Log.d("URL_SEARCH", url);

    // http post
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }

    // convert response to string
    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();
        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jArray = new JSONArray(result);

        Log.d("URL_SEARCH", "JSON: " + jArray.length());

    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }

    return jArray;
}