2017-04-23 51 views
0

這個程序是通過輸入一個id並向php在線文件發送請求並且響應的名字是 這個代碼有什麼問題?我沒有得到任何名稱Android - mysql php

XML

//activity_main.xml 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_height="495dp" 
     android:layout_width="368dp" 
     tools:layout_editor_absoluteY="8dp" 
     tools:layout_editor_absoluteX="8dp" 
     tools:ignore="MissingConstraints"> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <EditText 
       android:id="@+id/editTextId" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"/> 
      <Button 
       android:id="@+id/buttonGet" 
       android:text="Get" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 

     </LinearLayout> 
     <TextView 
      android:id="@+id/textViewResult" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 

    </LinearLayout> 

主類

//MainActivity.class 

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

    private EditText editTextId; 
    private Button buttonGet; 
    private TextView textViewResult; 

    private ProgressDialog loading; 

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

     editTextId = (EditText) findViewById(R.id.editTextId); 
     buttonGet = (Button) findViewById(R.id.buttonGet); 
     textViewResult = (TextView) findViewById(R.id.textViewResult); 

     buttonGet.setOnClickListener(this); 
    } 

    private void getData() { 
     String id = editTextId.getText().toString().trim(); 
     if (id.equals("")) { 
      Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show(); 
      return; 
     } 
     loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false); 

     String url = Config.DATA_URL+editTextId.getText().toString().trim(); 

     StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 
       loading.dismiss(); 
       showJSON(response); 
      } 
     }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Toast.makeText(MainActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show(); 
        } 
       }); 

     RequestQueue requestQueue = Volley.newRequestQueue(this); 
     requestQueue.add(stringRequest); 
    } 

    private void showJSON(String response){ 
     String name=""; 
     try { 
      JSONObject jsonObject = new JSONObject(response); 
      JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY); 
      JSONObject data = result.getJSONObject(0); 
      name = data.getString(Config.KEY_Name); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     textViewResult.setText("Name:\t"+name); 
    } 

    @Override 
    public void onClick(View v) { 
     getData(); 
    } 
} 

配置類

public class Config { 
    public static final String DATA_URL = "myPhpURL"; 
    public static final String KEY_Name = "name"; 
    public static final String JSON_ARRAY = "result"; 
} 

回答

0

如果您請求的是GET或POST,並且您未設置服務器的實際參數,則不會設置。您錯過了以下一段代碼

@Override 
protected Map<String, String> getParams() throws AuthFailureError { 
    Map<String, String> parameters = new HashMap<>(); 
     parameters.put("key", value); 
     return parameters; 
    }