下载并旁边上传在线数据库的数据

时间:2015-09-20 10:13:23

标签: java android mysql

我的项目有问题,我的altervista在线数据库已经填写并保存。但是当我尝试恢复数据并选择它们进入listview时,只收取表格的第一个元素和最后两个元素。此外,当这里的表的另一行无法为其充电时,它会尝试加载。表格如下: ID (int)自动增量,标题(文本),描述(longtext),正文(longtext),源(文本),日期(文本) 我用这个php脚本上传它:

<?php
        $host='localhost';
        $uname='root';   //Username per accedere nel database
        $pwd='';   //Password del database
        $db="my_notizieappandroid";   //Nome del database

                $select=$_REQUEST['id'];
        $con = mysql_connect($host,$uname,$pwd) or die("connection failed");
        mysql_select_db($db,$con) or die("db selection failed");

        $r=mysql_query($select,$con);

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

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

现在这是我的java代码。 Costanti.java

public class Costanti {

    private static String UrlLink = "http://www.mysite.altervista.org/";
    private static String PHPShow = "visualizza.php";
    private static String TABLE_NOTIZIE = "news";
    private static String KAY_ID = "ID";
    private static String KAY_TITLE = "title";
    private static String KAY_DESCRIPTION = "description";
    private static String KAY_BODY = "body";
    private static String KAY_SOURCE = "source";
    private static String KAY_DATE = "date";

    public static String getPHPShow() {
        return PHPShow;
    }

    public static String getKayDate() { return  KAY_DATE; }

    public static String getKaySource() {
        return KAY_SOURCE;
    }

    public static String getKayBody() {
        return KAY_BODY;
    }

    public static String getKayDescription() {
        return KAY_DESCRIPTION;
    }

    public static String getKayTitle() {
        return KAY_TITLE;
    }

    public static String getKayID() {
        return KAY_ID;
    }

    public static String getTableNotizie() {
        return TABLE_NOTIZIE;
    }

    public static String getUrlLink() {
        return UrlLink;
    }
}

CustomNewsAdapter.java:

public class CustomNewsAdapter extends ArrayAdapter<News> {

    private List<News> Object;
    private static final int TYPE_RIGHT = 1;

    @Override
    public int getViewTypeCount() {
        return 2;
    }
    public CustomNewsAdapter(Context C, List<News> Object){
        super(C, R.layout.news_row, Object);
        this.Object = Object;
    }

    @Override
    public View getView(int P, View convertV, ViewGroup parent){
        ViewHolder vHolder;
        News person = Object.get(P);
        if (convertV == null) {
            convertV = LayoutInflater.from(getContext()).inflate(R.layout.news_row,null);
            TextView title = (TextView) convertV.findViewById(R.id.title_news);
            TextView description = (TextView) convertV.findViewById(R.id.description_news);
            vHolder = new ViewHolder(title, description);
            convertV.setTag(vHolder);
        } else {
            vHolder = (ViewHolder) convertV.getTag();
        }
        vHolder.getTitle().setText(person.getTitle());
        vHolder.getDescription().setText(person.getDescription());

        return convertV;
    }

    public class ViewHolder {
        TextView title;
        TextView description;
        public ViewHolder(TextView title, TextView description){
            this.title = title;
            this.description = description;
        }

        public TextView getDescription() {
            return description;
        }

        public TextView getTitle() {
            return title;
        }
    }
}

News.java

public class News {

    private String title;
    private String description;
    private String source;
    private String body;
    private String date;
    private int ID;

    public News(int ID, String title, String description, String body, String source, String date) {
        this.ID = ID;
        this.title = title;
        this.description = description;
        this.body = body;
        this.source = source;
        this.date = date;
    }

    public String getDate() { return date; }

    public String getSource() {
        return source;
    }

    public String getBody() {
        return body;
    }

    public String getDescription() {
        return description;
    }

    public String getTitle() {
        return title;
    }
}

NewsActivity.java:

public class NewsActivity extends AppCompatActivity {

    Intent intent;

    private ProgressBar progressBar;
    private ListView listView;
    private TextView textView;
    private List<News> newses;

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

        progressBar = (ProgressBar) findViewById(R.id.progress);
        listView = (ListView) findViewById(R.id.list_view_news);
        textView = (TextView) findViewById(R.id.text);

        newses = new ArrayList<News>();
        QuerySelect();

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long offset) {

                News item = (News) newses.get(position);

                intent = new Intent(getApplicationContext(), NewsSelectedActivity.class);
                intent.putExtra("title", item.getTitle());
                intent.putExtra("description", item.getDescription());
                intent.putExtra("body", item.getBody());
                intent.putExtra("source", item.getSource());
                intent.putExtra("date", item.getSource());

                startActivity(intent);
            }
        });
    }

    private void QuerySelect() {
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        String Select = "SELECT * FROM news";
        params.put("id", Select);
        client.post(Costanti.getUrlLink() + Costanti.getPHPShow(), params, new AsyncHttpResponseHandler() {

            @Override
            public void onStart() {
                super.onStart();
                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                progressBar.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                String result = "";
                try {
                    result = new String(responseBody, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                JSONArray jsonArray = null;
                if (!result.equals("null")) {
                    try {
                        jsonArray = new JSONArray(result);
                        parsingJsonArray(jsonArray);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Toast.makeText(NewsActivity.this, "Connessione Fallita!", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void parsingJsonArray(JSONArray jsonArray) {
        try {
            for (int i=0; i<jsonArray.length(); i++) {
                JSONObject json_data = jsonArray.getJSONObject(i);
                newses.add(new News(json_data.getInt(Costanti.getKayID()), json_data.getString(Costanti.getKayTitle()),
                        json_data.getString(Costanti.getKayDescription()), json_data.getString(Costanti.getKayBody()),
                        json_data.getString(Costanti.getKaySource()), json_data.getString(Costanti.getKayDate())));
            }

        }catch (JSONException e) {
            e.printStackTrace();
        }
        listView.setAdapter(new CustomNewsAdapter(NewsActivity.this, newses));
    }
}

NewsSelectedActivity.java

public class NewsSelectedActivity extends AppCompatActivity {

    TextView textViewTitle;
    TextView textViewDescription;
    TextView textViewBody;
    TextView textViewSource;
    TextView textViewDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_selected);
        textViewTitle = (TextView) findViewById(R.id.title_news_selected);
        textViewDescription = (TextView) findViewById(R.id.description_news_selected);
        textViewBody = (TextView) findViewById(R.id.body_news_selected);
        textViewSource = (TextView) findViewById(R.id.source_news_selected);
        textViewDate = (TextView) findViewById(R.id.date_news_selected);


        String headline = "";
        String details = "";
        String text = "";
        String source = "";
        String date = "";

        Intent intent = getIntent();
        if (null != intent) {
            headline = intent.getStringExtra("title");
            details = intent.getStringExtra("description");
            text = intent.getStringExtra("body");
            source = intent.getStringExtra("source");
            date = intent.getStringExtra("date");
        }

        textViewTitle.setText(headline);
        textViewDescription.setText(details);
        textViewBody.setText(text);
        textViewSource.setText(source);
        textViewSource.setText(date);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
}

listview是这样的: 标题:今天我们没事 desription:null

title:null 描述:null

所以,如果我点击一个项目 标题:今天我们没事 desctription:null body:null 来源:www.today.com 日期:日期

0 个答案:

没有答案