我可以从我的Android应用程序将值注册到服务器。注册成功时,服务器会自动发送json响应,但我无法接收响应并在另一个活动中显示响应。例如,继承我的主要活动:
主要活动:
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.io.InputStream;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.ResponseCache;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
EditText emailview, numberview, pwview;
Button registerview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emailview = (EditText) findViewById(R.id.et1);
numberview = (EditText) findViewById(R.id.et2);
pwview = (EditText) findViewById(R.id.et3);
registerview = (Button) findViewById(R.id.btn1);
}
public void hit(View v) {
String email = emailview.getText().toString();
String contact = numberview.getText().toString();
String pw = pwview.getText().toString();
JSONObject a = new JSONObject();
try {
a.put("mail", email);
a.put("num", contact);
a.put("pass", pw);
} catch (JSONException e) {
e.printStackTrace();
}
if (a.length() > 0) {
new SendJsonDataToServer().execute(String.valueOf(a));
}
}
class SendJsonDataToServer extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String JsonResponse = null;
String JsonDATA = params[0];
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
try {
URL url = new URL("example.com");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
// is output buffer writter
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
//set headers and method
Writer writer = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8"));
writer.write(JsonDATA);
// json data
writer.close();
InputStream inputStream = urlConnection.getInputStream();
//input stream
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
while ((inputLine = reader.readLine()) != null)
buffer.append(inputLine + "\n");
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
JsonResponse = buffer.toString();
//response data
Log.i("o/p:", JsonResponse);
try {
//send to post execute
return JsonResponse;
} catch (Exception e){
}
return null;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("wtf", "Error closing stream", e);
}
}
}
return null;
}
@Override
protected void onPostExecute(String s) {
Log.i("Here it is:",s);
Log.e("Here it is:",s);
try {
JSONObject jsonResponse = new JSONObject(s);
int status= jsonResponse.getInt("status");
String message =jsonResponse.getString("message");
JSONArray arr = jsonResponse.getJSONArray("data");
if ( status==1){
for (int i=0;i<arr.length();i++) {
JSONObject lol = arr.getJSONObject(i);
String token = lol.getString("token");
String email = lol.getString("email");
Intent intent = new Intent(MainActivity.this, UserAreaActivity.class);
intent.putExtra("message", message);
intent.putExtra("token", token);
intent.putExtra("email", email);
startActivity(intent);
}}
else {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Registration failed").setNegativeButton("retry",null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}}
}
这是显示响应的责任活动
UserAreaActivity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class UserAreaActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_area);
setContentView(R.layout.activity_user_area);
final TextView tar =(TextView)findViewById(R.id.tv1);
final TextView zar =(TextView)findViewById(R.id.tv2);
final TextView par =(TextView)findViewById(R.id.tv3);
Intent intent=getIntent();
String message=intent.getStringExtra("message");
String token =intent.getStringExtra("token");
String email=intent.getStringExtra("email");
tar.setText(message);
zar.setText(token);
par.setText(email);
}
}