Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android中通过php从Json检索数据?_Java_Php_Android_Json - Fatal编程技术网

Java 在android中通过php从Json检索数据?

Java 在android中通过php从Json检索数据?,java,php,android,json,Java,Php,Android,Json,我有Json中的以下数据如何从该文件检索数据 {"first_name":"immi","last_name":"Ahmad","contact_no":"0333333","dob":"2010-09-29","gender":"Male","pro_id":"3"} 此外,我还使用http请求从android访问此文件。我希望获取每个字段并将其存储在android中的局部变量中。我尝试了以下操作,但没有成功 JSONObject json_data = new JSONObject(re

我有Json中的以下数据如何从该文件检索数据

{"first_name":"immi","last_name":"Ahmad","contact_no":"0333333","dob":"2010-09-29","gender":"Male","pro_id":"3"}
此外,我还使用http请求从android访问此文件。我希望获取每个字段并将其存储在android中的局部变量中。我尝试了以下操作,但没有成功

 JSONObject json_data = new JSONObject(result);
            json_data.getJSONObject("userinfo");
            json_data.getString("first_name");
这是我的安卓全班

public class ProfileActivity extends ActionBarActivity {

   //data for through http request
   static InputStream is = null;
    String result = "flag";
    String line = null;
    int userId;
    String action="select";
    String workTable="user";

    ArrayList<HashMap<String, String>> personList;

    //till here for through http request

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);


        userId = ((GlobalValues) this.getApplication()).getUserId();

        Toast.makeText(ProfileActivity.this, "user id is"+userId, Toast.LENGTH_SHORT).show();

       select();




        //for retrieval oncreate

    }
    public void select() {


        final ArrayList<HashMap<String, String>> personList;
        personList = new ArrayList<HashMap<String, String>>();

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

         nameValuePairs.add(new BasicNameValuePair("action", action));
        nameValuePairs.add(new BasicNameValuePair("worktable",workTable));
        nameValuePairs.add(new BasicNameValuePair("user_id",String.valueOf(userId)));



        try {
            HttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost("http......");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            Log.e("pass 1", "connection success ");
        } catch (Exception e) {
            Log.e("Fail 1", e.toString());
            Toast.makeText(getApplicationContext(), "Invalid IP Address",
                    Toast.LENGTH_LONG).show();
        }

        try {
            BufferedReader reader = new BufferedReader
                    (new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
            Log.e("pass 2", "connection success ");
        } catch (Exception e) {
            Log.e("Fail 2", e.toString());
        }

        try {
              JSONObject userinfo= new JSONObject(result);

        String fname=  userinfo.getString("first_name");
        Toast.makeText(ProfileActivity.this, "name is"+fname, Toast.LENGTH_SHORT).show();

            String fname=  userinfo.getString("first_name");
            Toast.makeText(ProfileActivity.this, "name is"+fname, Toast.LENGTH_SHORT).show();
           // JSONArray peoples = json_data.getJSONArray("userinfo");


           /* for (int i = 0; i < peoples.length(); i++) {

                JSONObject c = peoples.getJSONObject(i);
                String first_name = null, last_name = null, gender = null,contact_number=null,dob=null,profession_id=null;
                first_name = c.getString("first_name");
                last_name = c.getString("last_name");
                contact_number = c.getString("contact_no");
                dob=c.getString("dob");
                gender=c.getString("gender");
                profession_id=c.getString("pro_id");

                final HashMap<String, String> persons = new HashMap<String, String>();
                persons.put("firstName", first_name);
                persons.put("last_name", last_name);
                persons.put("contact_number", contact_number);
                persons.put("gender", gender);
                persons.put("dob", dob);
                persons.put("pro_id", profession_id);
                personList.add(persons);
                Toast.makeText(ProfileActivity.this, "your name is "+first_name, Toast.LENGTH_SHORT).show();

            }*/
        }catch (Exception e){
            Log.e("fil 3",e.toString());
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_profile, menu);
        return true;



    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        item.getItemId();
        switch (item.getItemId()) {
            case R.id.action_settings:
                goToHome();
                return true;



            default:
                return super.onOptionsItemSelected(item);
        }


    }

    public void goToHome(){

        Intent gotohome =new Intent(this,MapsActivity.class);
        startActivity(gotohome);
    }

    public void  goToEditProfile(View view){


        Intent gotoeditprofile=new Intent(this,TabedActivity.class);


        startActivity(gotoeditprofile);

    }

}

尝试使用
Gson
来简化它

这就是json文件的全部内容吗

您应该创建一个类来存储每个json对象的信息

您创建的示例PersonalInformation类具有实例“firstname”、“lastname”、“gender”

然后我们使用for循环语句读取所有信息


希望这有帮助

如果你的反应比@tiny sunlight方法大,那么这个方法就不起作用了

你不可能喜欢

     JSONObject userinfo= new JSONObject(result);
     userinfo.getString("first_name");
     JSONObject someObject = userinfo.getJsonObject("somthing")
     JSONObject somthingElse =   someObject.getJsonObject("somthingElse")
这会变得过于忙碌,几乎是在浪费你的时间

所以最好的答案是Gson Liabrary

Gson gson = new GsonBuilder().create();
Movie movie = gson.fromJson(response, Movie.class);
Heard movie是从json映射的类-在下面的链接中显示

你也可以做虎钳瓦尔萨

Gson gson = new GsonBuilder().create();
Responce mResponce = gson.fromJson(jsonStr , Responce .class);
String mName =mResponce.result.name;


希望您能理解哪个答案最好

#它向我展示了java.lang.String类型的异常值操作无法转换为JSONObject它的意思是什么?{“first_name”:“immi”,“last_name”:“Ahmad”,“contact_no”:“0333333”,“dob”:“2010-09-29”,“gender”:“Male”,“pro_id”:“3”}是你的结果吗?Plz上传异常屏幕截图。你能打印结果吗?谢谢。我抓住了错误。实际上它在我的php文件中。我必须使用POST而不是GET.thnx作为你的时间。
Gson gson = new GsonBuilder().create();
Movie movie = gson.fromJson(response, Movie.class);
Gson gson = new GsonBuilder().create();
Responce mResponce = gson.fromJson(jsonStr , Responce .class);
String mName =mResponce.result.name;