このような JSON 応答があります。
{"Fullname":" Prasad","vault_no":"12745"}.
これをandroid.Thisで文字列に変換する方法は、私のAndroidクラスファイルです。
Register.class
package com.example.test;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONObject;
import java.util.HashMap;
public class Register extends Activity implements View.OnClickListener{
private static final int[] CategoryCode = new int[]{0,1,2,3}; // array to save Salutation code
private static final String[] Category = new String[]{"Cartegory","Male","Female","Company"}; // array to show Salutation
private static final int[] SalutationCode = new int[]{0,1,2,3,4,5,6,7,8,9,0}; // array to save Salutation code
private static final String[] Salutation = new String[]{"Salutation","Mr","Ms","SME(small scale)",
"SU(start-up's)","SB(service based)","CA(charted accountant)","DR(doctor)",
"LC(large scale)","Others", "None"};
private static final int[] CountryCode = new int[]{0,91}; // array to save Country code
private static final String[] Country = new String[]{"Country","India"}; // array to show Country name.
private static final int[] CityCode = new int[]{0,1}; // array to save City code
private static final String[] City = new String[]{"City","Bangalore"}; // array to show City list
private static final int[] StateCode = new int[]{0,1}; // array to save State code
private static final String[] State = new String[]{"State","Karnataka"}; // array to show State list
int selectedAreaCode;
private Spinner spin;
private Spinner spin0;
private Spinner spin1;
private Spinner spin2;
private Spinner spin3;
private EditText editTextFname;
private EditText editTextLname;
private EditText editTextDob;
private EditText editTextPob;
private EditText editTextMobile;
private EditText editTextEmail;
private EditText editTextPin;
private Button register;
private Button login;
private static final String REGISTER_URL = "http://oursite/insert-db(1).php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayShowTitleEnabled(false);
setContentView(R.layout.activity_register_page);
spin = (Spinner)findViewById(R.id.spinner1);
spin0 = (Spinner)findViewById(R.id.spinner01);
spin1 = (Spinner)findViewById(R.id.spinner2);
spin2 = (Spinner)findViewById(R.id.spinner3);
spin3 = (Spinner)findViewById(R.id.spinner4);
editTextFname = (EditText) findViewById(R.id.editTextFname);
editTextLname = (EditText) findViewById(R.id.editTextLname);
editTextDob = (EditText)findViewById(R.id.editTextDob);
editTextPob = (EditText)findViewById(R.id.editTextPob);
editTextMobile = (EditText) findViewById(R.id.editTextMobile);
editTextEmail = (EditText)findViewById(R.id.editTextEmail);
editTextPin = (EditText) findViewById(R.id.editTextPin);
register = (Button) findViewById(R.id.button);
login = (Button) findViewById(R.id.button2);
register.setOnClickListener(this);
login.setOnClickListener(this);
ArrayAdapter<String> adpt_area = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Category);
adpt_area.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adpt_area);
ArrayAdapter<String> cat = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Salutation);
cat.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin0.setAdapter(cat);
ArrayAdapter<String> adpt_country = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Country);
adpt_country.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(adpt_country);
ArrayAdapter<String> adpt_city = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, City);
adpt_city.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(adpt_city);
ArrayAdapter<String> adpt_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, State);
adpt_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin3.setAdapter(adpt_state);
}
@Override
public void onClick(View v) {
if(v == register){
registerUser();
}
if(v == login){
startActivity(new Intent(this,LoginPage.class));
}
}
private void registerUser() {
String fname = editTextFname.getText().toString();
if(TextUtils.isEmpty(fname)) {
editTextFname.setError("please enter first name");
return;
}
String lname = editTextLname.getText().toString();
if(TextUtils.isEmpty(lname)) {
editTextLname.setError("please enter last name");
return;
}
String dob = editTextDob.getText().toString();
if(TextUtils.isEmpty(dob)) {
editTextDob.setError("enter date of birth");
return;
}
String pob = editTextPob.getText().toString();
if(TextUtils.isEmpty(pob)) {
editTextPob.setError("enter the place of birth");
return;
}
String mobile = editTextMobile.getText().toString();
if(TextUtils.isEmpty(mobile)) {
editTextMobile.setError("please enter mobile no");
return;
}
String email = editTextEmail.getText().toString();
if(TextUtils.isEmpty(email)) {
editTextEmail.setError("please enter the email id");
return;
}
String pin = editTextPin.getText().toString();
if(TextUtils.isEmpty(pin)) {
editTextPin.setError("please enter pin no");
return;
}
int selectedPosition = spin.getSelectedItemPosition();
// get area code based on value selected in spinner
String category = String.valueOf(CategoryCode[selectedPosition]);
int selectedPosition0 = spin0.getSelectedItemPosition();
// get area code based on value selected in spinner
String salutation = String.valueOf(SalutationCode[selectedPosition0]);
int selectedPosition1 = spin1.getSelectedItemPosition();
// get area code based on value selected in spinner
String country = String.valueOf(CountryCode[selectedPosition1]);
int selectedPosition2 = spin2.getSelectedItemPosition();
// get area code based on value selected in spinner
String state = String.valueOf(StateCode[selectedPosition2]);
int selectedPosition3 = spin3.getSelectedItemPosition();
// get area code based on value selected in spinner
String city = String.valueOf(CityCode[selectedPosition3]);
register(salutation,category,fname,lname,dob,pob,mobile,email,country,state,city,pin);
}
private void register(String salutation, String category, String fname, String lname,String dob,String pob,String mobile, String email,String country, String state, String city, String pin) {
class RegisterUser extends AsyncTask<String, Void, String>{
ProgressDialog loading;
//RegisterUserClass ruc = new RegisterUserClass();
RequestHandler ruc = new RequestHandler();
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(Register.this, "Please Wait",null, true, true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(),"Successfully Registered..!!",Toast.LENGTH_LONG).show();
editTextFname.setText("");
editTextLname.setText("");
editTextDob.setText("");
editTextPob.setText("");
editTextMobile.setText("");
editTextEmail.setText("");
editTextPin.setText("");
Intent i = new Intent(Register.this,UserProfile.class);
i.putExtra("s", s);
startActivity(i);
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<String,String>();
data.put("salutation",params[0]);
data.put("category",params[1]);
data.put("fname",params[2]);
data.put("lname",params[3]);
data.put("dob",params[4]);
data.put("pob",params[5]);
data.put("mobile",params[6]);
data.put("email", params[7]);
data.put("country",params[8]);
data.put("state",params[9]);
data.put("city",params[10]);
data.put("pin",params[11]);
String result = ruc.sendPostRequest(REGISTER_URL,data);
return result;
}
}
RegisterUser ru = new RegisterUser();
ru.execute(salutation,category,fname,lname,dob,pob,mobile,email,country,state,city,pin);
}
}
onPostExecute(String s)の文字列 " s "は JSON 値を保持します。文字列に変換して次のページに渡す方法は?
RequestHandler.class
package com.example.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class RequestHandler {
public String sendPostRequest(String requestURL,
HashMap<String, String> postDataParams) {
URL url;
StringBuilder sb = new StringBuilder();
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
sb = new StringBuilder();
String response;
while ((response = br.readLine()) != null){
sb.append(response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
}