2016-02-17 3 views
-2

Я пытаюсь создать программу, которая отобразит несколько названий с картинками под ними. Я просмотрел stackoverflow и не могу получить предложения для меня. Все мои ошибки в частном ничтожной ShowJSONНе удается разрешить конструктор JSONobject (Integer)

package br.exemplozxingintegration; 

import android.annotation.SuppressLint; 
import android.app.ProgressDialog; 
import android.content.ClipData; 
import android.content.ClipboardManager; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.android.volley.RequestQueue; 
import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.toolbox.StringRequest; 
import com.android.volley.toolbox.Volley; 
import com.squareup.picasso.Picasso; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 



public class ForthActivty extends AppCompatActivity implements View.OnClickListener { 

    private EditText pastetext; 
    private ClipboardManager myClipboard; 
    private ClipData myClip; 
    private Button btn; 
    private Button btn2; 
    private EditText textView1; 
    private Button buttonGet; 
    private TextView textViewResult; 
    private ImageView ImageView1; 

    private ProgressDialog loading; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_forth_activty); 
     myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
     pastetext = (EditText) findViewById(R.id.textView1); 

     btn = (Button)findViewById(R.id.buttonPaste); 
     btn.performClick(); 


     textView1 = (EditText) findViewById(R.id.textView1); 
     buttonGet = (Button) findViewById(R.id.buttonGet); 
     textViewResult = (TextView) findViewById(R.id.textViewResult); 
     ImageView1= (ImageView) findViewById(R.id.imageView1); 

     buttonGet.setOnClickListener(this); 

     btn2 = (Button)findViewById(R.id.buttonGet); 
     btn2.performClick(); 


    } 

    @SuppressLint("NewApi") 
    public void paste(View view) { 
     ClipData cp = myClipboard.getPrimaryClip(); 
     ClipData.Item item = cp.getItemAt(0); 
     String text = item.getText().toString(); 
     pastetext.setText(text); 
     Toast.makeText(getApplicationContext(), "", 
       Toast.LENGTH_SHORT).show();} 


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

     String url = ConfigRec.DATA_URL+textView1.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(ForthActivty.this,error.getMessage().toString(),Toast.LENGTH_LONG).show(); 
        } 
       }); 

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

    private void showJSON(Integer response){ 
    String title=""; 
    String image = ""; 
    try { 
     JSONArray jarray=new JSONArray(response); 
     for(response = 0;response<jarray.length();response++){ 
     JSONObject jsonObject = new JSONObject(response); 
     JSONArray result = jsonObject.getJSONArray(ConfigRec.JSON_ARRAY); 
     JSONObject androidData = result.getJSONObject(0); 
     title = androidData.getString(ConfigRec.KEY_TITLE); 
     image = androidData.getString(ConfigRec.KEY_IMAGE); 
    }} catch (JSONException e) { 
     e.printStackTrace(); 
    } 
     textViewResult.setText(title);//+"\nImagine :\t"+ image); 
     //int id = >getResources().getIdentifier("http://192.168.1.254/2015/380panel/uploads/images>/sm/"+ image, null, null); 
     //ImageView1.setImageURI(id); 
     >Picasso.with(this).load("http://192.168.1.254/2015/380panel/uploads/images/sm/+ image).into(ImageView1); 

    } 

И мои ошибки является:

Cannot resolve constructor JSONobject(java.lang.Integer) 

и это на линии JSONObject jsonObject = new JSONObject(response);

+0

в вашем цикле for showJSON отсутствуют фигурные скобки, следовательно, невозможно разрешить jsonObject –

+0

Are y мы пытаемся преобразовать ваш ответ в JSONArray? И что это должно означать JSONArray.length()? – ajantha

+0

Слишком много ошибок в коде. – Rohit5k2

ответ

0

Изменение showJson (Integer реакция) на

showJson(String response){ 
String title=""; 
String image = ""; 
try { 
    JSONArray jarray=new JSONArray(response); 
    for(int i= 0;i<jarray.length();i++){ 
    JSONObject jsonObject = jarray.getJSONObject(i); 
    JSONArray result = jsonObject.getJSONArray(ConfigRec.JSON_ARRAY); 
    JSONObject androidData = result.getJSONObject(0); 
    title = androidData.getString(ConfigRec.KEY_TITLE); 
    image = androidData.getString(ConfigRec.KEY_IMAGE); 
}} catch (JSONException e) { 
    e.printStackTrace(); 
} 
    textViewResult.setText(title);//+"\nImagine :\t"+ image); 
    //int id = >getResources().getIdentifier("http://192.168.1.254/2015/380panel/uploads/images>/sm/"+ image, null, null); 
    //ImageView1.setImageURI(id); 
    >Picasso.with(this).load("http://192.168.1.254/2015/380panel/uploads/images/sm/+ image).into(ImageView1); 

} 
+0

Спасибо большое, он работает без каких-либо ошибок, но он не отображается, когда я получаю активность. –

+0

Любые идеи, почему это ничего не отображает? –

+0

Покажите Json, который хотите разобрать. – ajantha

Смежные вопросы