如何在Android中传递值并获取json数据?

Dhirendra kumawat

我是android的新手,我想将json数据提取到我的device.but每当我试图传递值时,我都会收到html格式的未知错误。即使我没有在php代码中使用任何html标记。

    public class Messages2 extends Activity {

    private String[] navMenuTitles;
    private TypedArray navMenuIcons;
    private EditText editTextName;
    SharedPreferences sp;
    private String jsonResult;
    private ListView listView;
    private Button b;
    EditText etname, et;
    TextView tv;
    String myJSON;

    private static final String TAG_RESULTS="result";
    private static final String TAG_USERNAME = "username";
    private static final String TAG_NAME = "message_recd";
    private static final String TAG_ADD ="message_sent";

    JSONArray peoples = null;

    ArrayList<HashMap<String, String>> personList;

    ListView list;

    public static final String USER_NAME = "USERNAME";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Intent intent = getIntent();
        String fName = intent.getStringExtra("fname");



        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); 

        navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
        // load icons from
                                                            // strings.xml



        b=(Button)findViewById(R.id.Button01);
        list = (ListView) findViewById(R.id.listView);
        personList = new ArrayList<HashMap<String,String>>();


       b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = getIntent();
                String fName = intent.getStringExtra("fname");

                   getData(fName);

            }
        });
    }


        protected void showList(){
            try {
                JSONArray peoples = new JSONArray(myJSON);
                HashMap<String,String> person = new HashMap<String,String>();
                Intent intent1 = getIntent();
                String fName = intent1.getStringExtra("fname");
                person.put(TAG_NAME,fName);

                for(int i=0;i<peoples.length();i++){
                    JSONObject c = peoples.getJSONObject(i);
                    String name=null, address=null;
                    if(c.has("message_recd"))
                        name = c.getString("message_recd");
                    else if(c.has("message_sent"))
                        address = c.getString("message_sent");

                    HashMap<String,String> persons = new HashMap<String,String>();
                    persons.put(TAG_NAME,name);
                    persons.put(TAG_ADD,address);
                    personList.add(persons);
                }

                ListAdapter adapter = new SimpleAdapter(
                        Messages2.this, personList, R.layout.list_item,
                        new String[]{TAG_NAME,TAG_ADD},
                        new int[]{R.id.name, R.id.address}
                );

                list.setAdapter(adapter);

            } catch (JSONException e) {
                Log.i("tagconvertstr", "["+myJSON+"]");
            }
        }

        public void getData(String fName){
            class GetDataJSON extends AsyncTask<String, Void, String>{

                @Override
                protected String doInBackground(String... params) {
                    String paramUsername = params[0];
                    DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
                    HttpPost httppost = new HttpPost("http://10.0.2.2/progress_card/testing.php");

                    // Depends on your web service
                    httppost.setHeader("Content-type", "application/json");

                    InputStream inputStream = null;
                    String result = null;
                    try {
                        Intent intent1 = getIntent();
                        String fName = intent1.getStringExtra("fname");

                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                        nameValuePairs.add(new BasicNameValuePair("username", fName));

                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();

                        inputStream = entity.getContent();
                        // json is UTF-8 by default
                        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                        StringBuilder sb = new StringBuilder();

                        String line = null;
                        while ((line = reader.readLine()) != null)
                        {
                            sb.append(line + "\n");
                        }
                        result = sb.toString();
                    } catch (Exception e) {
                        Log.i("tagconvertstr", "["+result+"]");
                    }
                    finally {
                        try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                    }
                    return result;
                }

                @Override
                protected void onPostExecute(String result){
                    myJSON=result;
                    showList();
                }
            }
            GetDataJSON g = new GetDataJSON();
            g.execute(fName);
        }

}

我的Logcat

09-24 06:44:09.973: I/tagconvertstr(3609): [<br />
09-24 06:44:09.973: I/tagconvertstr(3609): <font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
09-24 06:44:09.973: I/tagconvertstr(3609): <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: username in G:\wamp\www\progress_card\testing.php on line <i>7</i></th></tr>
09-24 06:44:09.973: I/tagconvertstr(3609): <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
09-24 06:44:09.973: I/tagconvertstr(3609): <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
09-24 06:44:09.973: I/tagconvertstr(3609): <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>382904</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='G:\wamp\www\progress_card\testing.php' bgcolor='#eeeeec'>..\testing.php<b>:</b>0</td></tr>
09-24 06:44:09.973: I/tagconvertstr(3609): </table></font>
09-24 06:44:09.973: I/tagconvertstr(3609): [{"message_recd":""},{"message_recd":""},{"message_recd":""},{"message_recd":""},{"message_recd":""},{"message_recd":""},{"message_sent":""},{"message_sent":""},{"message_sent":""},{"message_sent":""},{"message_sent":""},{"message_sent":""}]
09-24 06:44:09.973: I/tagconvertstr(3609): ]

我的PHP代码

<?php
define('HOST','localhost');
  define('USER','root');
  define('PASS','');
  define('DB','progress_card');
  $con = mysqli_connect(HOST,USER,PASS,DB);
  $username = $_POST['username'];
$sql1 = "select * from student_detail where parentusername='".$username."'";
$res1 = mysqli_query($con,$sql1);

$row1=mysqli_fetch_array($res1);

$cl=$row1['class']."-".$row1['section'];
$sql2="select * from teachers where classassign='$cl'";
$res2 = mysqli_query($con,$sql2);

$row2=mysqli_fetch_array($res2);

$to=$row2['email'];
$from=$row1['parentemail'];

$result = array();

$sql = "select * from messages where to_email='".$to."' and from_email='".$from."'";
$res = mysqli_query($con,$sql);

while($row = mysqli_fetch_array($res))
{
array_push($result,array('message_recd'=>$row['message']));
}

$sqlw = "select * from messages where from_email='".$to."' and to_email='".$from."'";
$resw = mysqli_query($con,$sqlw);

while($row5 = mysqli_fetch_array($resw))
{

array_push($result,array('message_sent'=>$row5['message']));
}
if(mysqli_query($con,$sql))
{
   // echo 'success';
  }
  else
  {
    //echo 'failure';
  }
//array_push($result,array('message_sent'=>"dfdsghdfgddfgdsd"));
//array_push($result,array('message_sent'=>"sfdsflkufhskfhdskjfsfssadfadsffsafasfsfsadfafsaf"));

//array_push($result,array('message_recd'=>$row1['parentemail'],'message_sent'=>$row2['email']));

echo json_encode($result);

mysqli_close($con);

?>
贾南德拉·玛尼(Gyanendra mani)

您的网址只需要返回json响应,否则它将无法在android中解析。仅此部分必须作为响应返回:

[{"message_recd":""},{"message_recd":""}]

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Android适配器中传递Json数据

来自分类Dev

如何在Android的地图活动中从URL获取JSON数据

来自分类Dev

如何在JSON中获取随机值

来自分类Dev

如何在 JSON 中获取键的值

来自分类Dev

而不是JSON数据链接返回.txt文件如何在android中获取JSON数据

来自分类Dev

如何在python中获取json的数据

来自分类Dev

如何在Android中获取JsonResponse的值?

来自分类Dev

Android:如何在从选项卡布局中传递数据时显示其值

来自分类Dev

如何在Android中从XML获取数据

来自分类Dev

如何在jquery函数中传递值以检索数据

来自分类Dev

如何在jsonstore Worklight 6.2中以Json格式从数据库获取或返回值?

来自分类Dev

如何在Android中的不同textview中传递数据意图

来自分类Dev

如何在Android中检索JSON值?

来自分类Dev

如何在json格式数据集中获取最高值?

来自分类Dev

如何在Android Studio中的数据绑定中将值传递给android.view.View.OnClickListener变量?

来自分类Dev

如何在Angular Js中通过Ajax传递JSON数据

来自分类Dev

如何在jstree JSON数据中获取数据ID

来自分类Dev

如何在从数据库中获取数据时仅获取最后一次迭代值,我尝试过将值作为 JSON 格式获取

来自分类Dev

如何在Android中获取jsonObject值的值?

来自分类Dev

如何在android中的片段之间传递值?

来自分类Dev

Android片段:如何在XML中传递值

来自分类Dev

如何在Json ..中获取JSON对象的值?

来自分类Dev

如何在我的 JSON 对象中获取相应的 JSON 值?

来自分类Dev

如何在Android中获取和转换自定义JSON数据作为字符串

来自分类Dev

WordPress 插件:如何在 JSON 中获取数据并在 android 中使用?

来自分类Dev

如何在Android中的多个片段之间传递数据

来自分类Dev

如何在 Android 中的 ViewPager 中正确传递数据

来自分类Dev

如何在javascript中的数组中获取json值

来自分类Dev

如何在javascript中以json格式从数组中获取数据

Related 相关文章

  1. 1

    如何在Android适配器中传递Json数据

  2. 2

    如何在Android的地图活动中从URL获取JSON数据

  3. 3

    如何在JSON中获取随机值

  4. 4

    如何在 JSON 中获取键的值

  5. 5

    而不是JSON数据链接返回.txt文件如何在android中获取JSON数据

  6. 6

    如何在python中获取json的数据

  7. 7

    如何在Android中获取JsonResponse的值?

  8. 8

    Android:如何在从选项卡布局中传递数据时显示其值

  9. 9

    如何在Android中从XML获取数据

  10. 10

    如何在jquery函数中传递值以检索数据

  11. 11

    如何在jsonstore Worklight 6.2中以Json格式从数据库获取或返回值?

  12. 12

    如何在Android中的不同textview中传递数据意图

  13. 13

    如何在Android中检索JSON值?

  14. 14

    如何在json格式数据集中获取最高值?

  15. 15

    如何在Android Studio中的数据绑定中将值传递给android.view.View.OnClickListener变量?

  16. 16

    如何在Angular Js中通过Ajax传递JSON数据

  17. 17

    如何在jstree JSON数据中获取数据ID

  18. 18

    如何在从数据库中获取数据时仅获取最后一次迭代值,我尝试过将值作为 JSON 格式获取

  19. 19

    如何在Android中获取jsonObject值的值?

  20. 20

    如何在android中的片段之间传递值?

  21. 21

    Android片段:如何在XML中传递值

  22. 22

    如何在Json ..中获取JSON对象的值?

  23. 23

    如何在我的 JSON 对象中获取相应的 JSON 值?

  24. 24

    如何在Android中获取和转换自定义JSON数据作为字符串

  25. 25

    WordPress 插件:如何在 JSON 中获取数据并在 android 中使用?

  26. 26

    如何在Android中的多个片段之间传递数据

  27. 27

    如何在 Android 中的 ViewPager 中正确传递数据

  28. 28

    如何在javascript中的数组中获取json值

  29. 29

    如何在javascript中以json格式从数组中获取数据

热门标签

归档