如何解决这个问题? [英] How to resolve this problem?

查看:78
本文介绍了如何解决这个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSONActivity.java

JSONActivity.java

package com.example.androiddbconnection;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import com.example.androiddbconnection.CustomHttpClient;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class JSONUseActivity extends Activity {
 EditText byear;   // To take birthyear as input from user
 Button submit;    
 TextView tv;      // TextView to show the result of MySQL query 
 
 String returnString;   // to store the result of MySQL query after decoding JSON
 

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used to catch accidental disk or network access on the application's main thread
.penaltyLog().build());
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_list_item);
     
     byear = (EditText) findViewById(R.id.button1);
     submit = (Button) findViewById(R.id.button2);
     tv = (TextView) findViewById(R.id.button3);
             
     // define the action when user clicks on submit button
     submit.setOnClickListener(new View.OnClickListener(){        
      public void onClick(View v) {
       // declare parameters that are passed to PHP script i.e. the name "birthyear" and its value submitted by user   
       ArrayList<namevaluepair> postParameters = new ArrayList<namevaluepair>();
       
       // define the parameter
       postParameters.add(new BasicNameValuePair("id",
   byear.getText().toString()));
       String response = null;
       
       // call executeHttpPost method passing necessary parameters 
       try {
  response = CustomHttpClient.executeHttpPost(
    
    "http://localhost/byear.php",  // in case of a remote server
    postParameters);
  
  // store the result returned by PHP script that runs MySQL query
  String result = response.toString();  
           
   //parse json data
      try{
              returnString = "";
        JSONArray jArray = new JSONArray(result);`enter code here`
              for(int i=0;i<jarray.length();i++){>
                      JSONObject json_data = jArray.getJSONObject(i);
                      Log.i("log_tag","id: "+json_data.getInt("id")+
                              ", name: "+json_data.getString("name")+
                              ", place: "+json_data.getString("place")
                           
                      );
                      //Get an output to the screen
                      returnString += "\n" + json_data.getString("name") + " -> "+ json_data.getString("place");
              }
      }
      catch(JSONException e){
              Log.e("log_tag", "Error parsing data "+e.toString());
      }
  
      try{
       tv.setText(returnString);
      }
      catch(Exception e){
       Log.e("log_tag","Error in Display!" + e.toString());;          
      }   
 }
       catch (Exception e) {
  Log.e("log_tag","Error in http connection!!" + e.toString());     
 }
      }         
     });
 }
}



activity_jsonuse.xml


activity_jsonuse.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

    <textview

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="BirthYear" />

    <edittext

        android:id="@+id/editText1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:inputType="text" >        
    </edittext>

    <Button

        android:id="@+id/submitbutton"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Submit"

        android:layout_gravity="center"

         />
 <textview

        android:id="@+id/showresult"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Result" />
</linearlayout>





byear.php



byear.php

$host='localhost';
$uname='root';
$pwd='';
$db="android";

$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");


$sql = "SELECT * FROM users WHERE  id > '". $_POST["id"]."'";


while($row=mysql_fetch_array($sql))
{
    $flag[name]=$row[name];
}

print(json_encode($flag));
mysql_close($con);





请帮助我。 。谢谢



pls help me. . Thank you

推荐答案

host = ' localhost';
host='localhost';


uname = ' root';
uname='root';


pwd = ' ';


这篇关于如何解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆