我有三个要发送给MainActivity的值。 JSONObject json到达时带有正确的值,但是在try catch中未访问此值,即onPostExecute仅执行pDialog.dismiss();。
我如何获取onPostExecute以便在内部读取尝试小插曲?
MainActivity
package ic.eng.br.testejson;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity implements Json{
EditText edtUsuario, edtSenha;
Button btnLogin;
TextView textView1, textView2, textView3;
JSONArray user = null;
String url = "http://www.ic.eng.br/android/login.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtUsuario = findViewById(R.id.edtUsuario);
edtSenha = findViewById(R.id.edtSenha);
btnLogin = findViewById(R.id.btnLogin);
textView1 = findViewById(R.id.textView1);
textView2 = findViewById(R.id.textView2);
textView3 = findViewById(R.id.textView3);
final Asyncexecute asyncexecute = new Asyncexecute(this, this);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String usuario = edtUsuario.getText().toString();
String senha = edtSenha.getText().toString();
asyncexecute.execute(usuario, senha, url, "");
}
});
}
public void testejson (String Rsenha, String Rprivilegio, String Rperfil) {
textView1.setText(Rsenha);
textView2.setText(Rprivilegio);
textView3.setText(Rperfil);
}
}
异步执行:
package ic.eng.br.testejson;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
@SuppressWarnings("deprecation")
public class Asyncexecute extends AsyncTask <String, String, JSONObject> {
private ProgressDialog pDialog;
private Context context;
JSONArray user = null;
String Rsenha, Rprivilegio, Rperfil;
String usuario, senha, url;
private static final String TAG_USER = "usuario";
private Json execinterface;
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public Asyncexecute(Context context, Json execinterface) {
this.execinterface = execinterface;
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Carregando");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... params) {
usuario = params[0];
senha = params[1];
url = params[2];
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);;
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
user = json.getJSONArray("login");
JSONObject c = user.getJSONObject(0);
Rsenha = c.getString("senha");
Rprivilegio = c.getString("privilegio");
Rperfil = c.getString("perfil");
execinterface.testejson(Rsenha, Rprivilegio, Rperfil);
} catch (JSONException e){
e.printStackTrace();
}
}
}
JSONParser:
package ic.eng.br.testejson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("login", "tiago"));
nameValuePairs.add(new BasicNameValuePair("senha", "123456"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
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();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
Joson
package ic.eng.br.testejson;
import android.view.View;
import org.json.JSONException;
import org.json.JSONObject;
public interface Json {
void testejson (String Rsenha, String Rprivilegio, String Rperfil);
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/edtUsuario"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textColor="#D3D3D3"
tools:ignore="Autofill,LabelFor,TextFields"
android:maxLines="1"
android:singleLine="true"
android:digits="abcdefghijklmnopqrstuvxywzç"/>
<EditText
android:id="@+id/edtSenha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="15" />
<Button
android:id="@+id/btnLogin"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Efetuar Login"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>