這是我得到的JSON文件中的網頁代碼:更新JSONFile託管Web服務器上
package com.example.maclocation;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
String[] n1;
String[] n2;
String[] n3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ActorsAsyncTask().execute("link here");
}
class ActorsAsyncTask extends AsyncTask<String, Void, Boolean> {
這是我的代碼用於獲取JSON文件網址:
@Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpPost post = new HttpPost(urls[0]);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.optJSONArray("coordinates");
n1 = new String[jarray.length()];
n2 = new String[jarray.length()];
n3 = new String[jarray.length()];
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
n1[i] = object.getString("place").toString();
n2[i] = object.getString("lat").toString();
n3[i] = object.getString("lng").toString();
}
return true;
}
//------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
這是我的
protected void onPostExecute(Boolean result) {
if(result == false){
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}else{
for (int i = 0; i < n1.length; i++) {
// String xss= actorsList.
Toast.makeText(getApplicationContext(), n1[i] + "*" + n2[i]+ "*" +n3[i] , Toast.LENGTH_LONG).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;
}
}
問:
01獲得在網絡JSON文件代碼是否可以更新託管在Web服務器中的JSON文件?像更新我的當前位置?我正在使用ADT Eclipse。
你想通過應用程序更新json文件? – calvinfly
是的,它是可行的? – DefineMc
@calvinfly是否有可能使用我的應用程序更新我的服務器json? – DefineMc