使用我的项目android解析json后,即使我添加了权限INTERNET和READ PHONE STATES,结果也不会显示在对话框中 我希望有人能帮助我解决这个问题,我已经为许多尝试感到厌倦,没有找到解决方案,这是我的代码:
Json
{
"Dialog": "welcome to my app"
}
getfromjson.java
import android.os.AsyncTask;
import android.util.Log;
import org.json.JSONObject;
public class getfromjson extends AsyncTask {
private String dialog = "";
@Override
protected Object doInBackground(Object... params) {
this.parse();
return this;
}
private void parse()
{
Gethttp sh = new Gethttp();
String url = "https://api.myjson.com/bins/1902bf";
String jsonStr = sh.makeServiceCall(url);
try {
JSONObject jsonRoot = new JSONObject(jsonStr);
String warningString = jsonRoot.getString("Dialog");
dialog = warningString;
}
catch(Exception e)
{
}
}
public String getDialog()
{
return dialog;
}
}
Gethttp.java
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
public class Gethttp {
private static final String TAG = Gethttp.class.getSimpleName();
public Gethttp() {
}
public String makeServiceCall(String reqUrl) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
opendialog.java
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatDialogFragment;
import java.util.concurrent.ExecutionException;
public class opendialog extends AppCompatDialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final getfromjson parsejson = new getfromjson();
try {
parsejson.execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
builder.setTitle(getResources().getString(R.string.Message1))
.setIcon(R.drawable.warning)
.setMessage(parsejson.getDialog())
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
}
答案 0 :(得分:0)
问题是因为您尝试在doInBackground()
AsyncTask运行时同时显示对话框。 AsyncTask
尚未收到回复,您已经显示了对话框。
这是怎么做:
尝试在onPostExecute()
类中覆盖getfromjson
,然后在此处放置显示对话框的代码。
另外,如果您使用普通的AppCompatActivity
或Fragment
而不是AppCompatDialogFragment
进行创建,然后在其中显示对话框,则这样做会更容易。
修改:
创建一个名为OpenDialogActivity的新活动,并将以下内容复制到OpenDialogActivity Java类中,我将所有单独的Java类合并为一个类:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open_dialog);
final getfromjson parsejson = new getfromjson();
parsejson.execute();
}
private class getfromjson extends AsyncTask {
private String dialog = "";
@Override
protected Object doInBackground(Object... params) {
this.parse();
return this;
}
private void parse()
{
Gethttp sh = new Gethttp();
String url = "https://api.myjson.com/bins/1902bf";
String jsonStr = sh.makeServiceCall(url);
try {
JSONObject jsonRoot = new JSONObject(jsonStr);
String warningString = jsonRoot.getString("Dialog");
dialog = warningString;
}
catch(Exception e)
{
e.printStackTrace();
}
}
public String getDialog()
{
return dialog;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
AlertDialog.Builder builder = new AlertDialog.Builder(OpenDialogActivity.this);
builder.setTitle(getResources().getString(R.string.Message1))
.setIcon(R.drawable.warning)
.setMessage(getDialog())
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
}
public class Gethttp {
private final String TAG = Gethttp.class.getSimpleName();
public Gethttp() {
}
public String makeServiceCall(String reqUrl) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}