我有一个案例,当使用 jsonparser 向 API 发出请求时,我的第二台服务器会得到不同的结果。总是失败。。
在两台服务器(服务器 1 和 2)上,我制作了一个完全相同数据的示例,但是当我在应用程序上发出请求时,第一台服务器会打开以良好响应,但第二台服务器总是以null
. 如何使用第二台服务器才能正常运行?我对这个案子很困惑..
在上述情况下,我做了一个简单的应用程序来进行实验:
package com.joris.androidjsonparsingfromurl;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView uid;
TextView name1;
TextView email1;
Button Btngetdata;
// URL to get JSON Array
//private static String url = "http://newapi.bacaberita.com/statis.php";
private static String url = "http://api.berthojoris.com/babe/statis.php";
// JSON Node Names
private static final String TAG_ITEM = "items";
private static final String TAG_ISDISPLAY = "IsDisplay";
private static final String TAG_TITLE = "Title";
private static final String TAG_POST = "Post";
JSONArray user = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Utils.setPolicyThread();
Btngetdata = (Button) findViewById(R.id.getdata);
Btngetdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new JSONParse().execute();
}
});
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
uid = (TextView) findViewById(R.id.uid);
name1 = (TextView) findViewById(R.id.name);
email1 = (TextView) findViewById(R.id.email);
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.setCanceledOnTouchOutside(false);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
if (result.length() > 0) {
pDialog.dismiss();
try {
// Getting JSON Array
user = result.getJSONArray(TAG_ITEM);
JSONObject c = user.getJSONObject(0);
// Storing JSON item in a Variable
String status = c.getString(TAG_ISDISPLAY);
String title = c.getString(TAG_TITLE);
String post = c.getString(TAG_POST);
// Set JSON Data in TextView
uid.setText(status);
name1.setText(title);
email1.setText(post);
Log.e("HASILNYA", status+title+post);
} catch (JSONException e) {
e.printStackTrace();
}
}else{
pDialog.dismiss();
Toast.makeText(getBaseContext(),
"Data Kosong Gan...", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage(R.string.really_quit)
.setPositiveButton(R.string.yes,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
finish();
}
}).setNegativeButton(R.string.no, null).show();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
}
有什么不同导致我无法向第二台服务器发出请求吗?请帮忙..谢谢