gpt4 book ai didi

java - 如何在 Java 中用 JSON 生成 "raw"字符串

转载 作者:行者123 更新时间:2023-11-29 00:25:24 27 4
gpt4 key购买 nike

我正在为学校 atm 编写一个 android 应用程序。我有股票,想请你帮我。到目前为止,我了解如何使用 PHP 从 MySQL 数据库中打印数据,准备好用 Java 进行解析。然后是我的代码(取自此处:http://www.helloandroid.com/tutorials/connecting-mysql-database)takes 数据并将其转换为我认为的字符串。

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

这是我使用的 Activity (登录表单)的所有代码:

package com.example.fungi;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;




public class MainActivity extends Activity {
//Intent i = new Intent(getBaseContext(), MainPageActivity.class);
//startActivity(i);
Button login;
EditText username;
EditText password;
TextView lol;
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ActionBar actionBar = getActionBar();
//actionBar.hide();
lol = (TextView) findViewById(R.id.textView1);
login = (Button) findViewById(R.id.loginbutton);
username = (EditText) findViewById(R.id.usernametext);
password = (EditText) findViewById(R.id.passwordtext);
login.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String usernameget = username.getText().toString();
String passwordget = password.getText().toString();

if(usernameget.contentEquals("")){
username.setText("usernam required");
}else {
if(passwordget.contentEquals("")){
username.setText("password required");
}else {
String userid = username.getText().toString();
Intent intent = new Intent(getBaseContext(), Side.class);
intent.putExtra("sessionid", userid);
startActivity(intent);

}

}

}

});
String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair(result, result));

//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://http://fungi.webatu.com/Index.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
}catch(Exception e11){
Log.e("log_tag", "Fejl i HTTP connection "+e11.toString());
}

//convert response to string
try{

BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),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", "Fejl i resultat konvertering "+e.toString());
}

//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("id")+
", username: "+json_data.getString("username")+
", password: "+json_data.getString("password")+
", alder: "+json_data.getInt("alder"));


//maybe if statement here?


;
}

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


}



}

提前致谢!!

最佳答案

我不确定你的问题,但我认为你想从网络服务中获取一些 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;
}

关于java - 如何在 Java 中用 JSON 生成 "raw"字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19167374/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com