如何从内部存储中读取json数组并发送http请求?

时间:2016-10-19 06:07:41

标签: android json

我需要澄清http connection.i可以从内部memory.how读取我的json对象发送http请求吗?  我必须在发送数据之后同时发送发送请求,之后它将变为空,直到下一个数据到达该文件。 这里我的代码是

package example.json.com.jsonarray;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

 public class MainActivity extends Activity {
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView)findViewById(R.id.textView);
    StringBuilder sb = new StringBuilder();
    String data="";
    try {
        BufferedReader  br = new BufferedReader(new FileReader(new File(Environment.getExternalStorageDirectory(),"mservice/database/queue_mgr.txt")));
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        br.close();
        try {
            JSONArray  jsonArray = new JSONArray(sb.toString());
            //Iterate the jsonArray and print the info of JSONObjects
            for(int i=0; i < jsonArray.length(); i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                data+=jsonObject.toString();
            }
            Toast.makeText(this, data, Toast.LENGTH_LONG).show();
            textView.setText("");
            textView.append("\n\n-----START CONTENT-----\n\n");
            textView.append(data);
        } catch (JSONException e) {e.printStackTrace();}

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

0 个答案:

没有答案