我正在为学校 atm 编写一个 android 应用程序。我是股票,想请你帮我。到目前为止,我了解如何使用 PHP 打印 MySQL 数据库中的数据,以便在 Java 中进行解析。然后我的代码(取自这里:http ://www.helloandroid.com/tutorials/connecting-mysql-database)获取数据并将其转换为我相信的字符串。
所以我的问题是:我如何为 eksample make 和 if 语句检查用户数据是否正确?我就是不明白!
这是我使用的活动(登录表单)的所有代码:
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());
}
}
}
提前致谢!!