没有从服务器获取数据到android中的textview?

时间:2017-01-26 06:53:28

标签: java android mysql textview

我想设置文本来自服务器的数据,我在调试时得到了服务器的响应,但是它没有设置settext。当我运行代码时似乎没有发生任何事情,它没有将服务器数据显示到textview: 这是我的java类:

public class PMAddPigeonActivity extends AppCompatActivity {

private static String url= "http://.........php";
    TextView commission;

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

  commission=(TextView)findViewById(R.id.comm) ;


        getData();
}
private void getData() {
        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                showJSON(response);
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(PMAddPigeonActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                    }
                });

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

    private void showJSON(String response){


        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
            JSONObject collegeData = result.getJSONObject(0);
           String name1 = collegeData.getString("commission");
            commission.setText(name1, TextView.BufferType.EDITABLE );





        } catch (JSONException e) {
            e.printStackTrace();
        }
       // commission.setText(""+name );
    }

config.java

public class Config {

    public static final String KEY_NAME = "commission";

    public static final String JSON_ARRAY = "result";
}

我的json输出:

{
  "id": 1,
  "commission": 0.01
}

我的xml:

<TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"

                android:id="@+id/comm"
                android:textColor="#000"
                android:layout_marginTop="70dp"

                android:layout_alignTop="@+id/pedigreeupload"
                android:layout_alignRight="@+id/dname"
                android:layout_alignEnd="@+id/dname" />

1 个答案:

答案 0 :(得分:1)

由于您的回复中没有JSON数组,因此您需要更改响应处理,如下所示。

 JSONObject jsonObject = new JSONObject(response);
 String name1 = jsonObject.getString("commission");
 commission.setText(name1, TextView.BufferType.EDITABLE );