2015-06-23 161 views
0

这是我得到的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。

+0

你想通过应用程序更新json文件? – calvinfly

+0

是的,它是可行的? – DefineMc

+0

@calvinfly是否有可能使用我的应用程序更新我的服务器json? – DefineMc

回答

0

所以,你需要把它在服务器端,使用PHP的可能,红宝石,蟒蛇,或者您可以使用服务器上的任何其他语言代码..

然后通过应用程序,你可以做一个http请求传递一些参数以确定您在JSON文件托管的同一台服务器上执行的操作(以便您可以访问它),然后您可以对其进行编辑!

本教程是非常好的:http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/

在这种情况下,它是一个数据库,而不是一个JSON文件,但这个想法是一样的。

+0

哦,太棒了,谢谢,现在我的问题是,我没有主意,当涉及到PHP – DefineMc

+0

[Here](http://stackoverflow.com/questions/17806224/how-to-update-edit-json-file-using -php)和[here](http://stackoverflow.com/questions/6313276/php-how-to-update-json-data-in-a-txt-file)。这很容易,基本上你需要使用json_decode()和json_encode()函数。 –

+0

因此,您可以拥有一个从应用程序读取请求(可以是POST或GET)的php页面,例如:http://www.example.com/?action=write_json&content=something。 php读取动作,如果它是'write_json',那么你得到的是'something'的内容并将其写入json文件。 –