开机自检未在Android上发生?

狮子789

嘿,所以我没有收到错误消息,但是我的所有日​​志都已初始化,除了HttpResponse之后的日志不确定原因之外,在服务器端,我看不到POST的任何活动。

这是我的代码:

package com.sfsfdsfds;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class wardrobe extends Activity{

    //set variable for the fields
    private EditText nameField;
    private Spinner typeField;
    private EditText colorField;
    private Spinner seasonField;
    private EditText sizeField;
    private EditText quantityField;
    private ImageView imageField;
    private ProgressBar progressBarField;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wardrobe);
        ImageView user_photo = (ImageView) findViewById(R.id.user_photo);

        //button for upload image
        Button uploadImageButton = (Button) findViewById(R.id.uploadImageButton);

        //button for posting details
        Button postWardrobe = (Button) findViewById(R.id.postButton);

        //Value of fields
        nameField = (EditText) findViewById(R.id.nameFieldWardrobeScreen);
        typeField = (Spinner) findViewById(R.id.typeFieldWardrobeScreen);
        colorField = (EditText) findViewById(R.id.colorFieldWardrobeScreen);
        seasonField = (Spinner) findViewById(R.id.seasonFieldWardrobeScreen);
        sizeField = (EditText) findViewById(R.id.sizeFieldWardrobeScreen);
        quantityField = (EditText) findViewById(R.id.quantityFieldWardrobeScreen);
        imageField = (ImageView) findViewById(R.id.user_photo);
        progressBarField = (ProgressBar) findViewById(R.id.progressBarWardrobe);
        progressBarField.setVisibility(View.GONE);


        //Creating spinner for select/options for type field
        Spinner spinnerType = (Spinner) findViewById(R.id.typeFieldWardrobeScreen); 
        ArrayAdapter<CharSequence> adapterTypeArray = ArrayAdapter.createFromResource(this,  R.array.type_array, android.R.layout.simple_spinner_item);
        adapterTypeArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerType.setAdapter(adapterTypeArray);

      //Creating spinner for select/options for season field
        Spinner spinnerSeason = (Spinner) findViewById(R.id.seasonFieldWardrobeScreen); 
        ArrayAdapter<CharSequence> adapterSeasonArray = ArrayAdapter.createFromResource(this,  R.array.season_array, android.R.layout.simple_spinner_item);
        adapterSeasonArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerSeason.setAdapter(adapterSeasonArray); 

       uploadImageButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

                //below allows you to open the phones gallery
                Image_Picker_Dialog();
            }

        });


       postWardrobe.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                    //validate input and that something was entered
                    if(nameField.getText().toString().length()<1 || colorField.getText().toString().length()<1 || sizeField.getText().toString().length()<1 || quantityField.getText().toString().length()<1) {

                        //missing required info (null was this  but lets see)
                        Toast.makeText(getApplicationContext(), "Please complete all sections!", Toast.LENGTH_LONG).show();
                    } else {
                        JSONObject dataWardrobe = new JSONObject();

                        try {
                            dataWardrobe.put("type", typeField.getSelectedItem().toString());
                            dataWardrobe.put("color", colorField.getText().toString());
                            dataWardrobe.put("season", seasonField.getSelectedItem().toString());
                            dataWardrobe.put("size", sizeField.getText().toString());
                            dataWardrobe.put("quantity", quantityField.getText().toString());               

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        //make progress bar visible
                        progressBarField.setVisibility(View.VISIBLE);

                        //execute the post request
                        new dataSend().postData(dataWardrobe);
                    }

                    //below should send data over

                }

            });    
    }

    // After the selection of image you will retun on the main activity with bitmap image
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
            {
                    super.onActivityResult(requestCode, resultCode, data);
                    if (requestCode == Utility.GALLERY_PICTURE)
                            {
                                    // data contains result
                                    // Do some task
                                    Image_Selecting_Task(data);
                            } else if (requestCode == Utility.CAMERA_PICTURE)
                            {
                                    // Do some task
                                    Image_Selecting_Task(data);
                            }
            }
    public void Image_Picker_Dialog()
    {

        AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
        myAlertDialog.setTitle("Pictures Option");
        myAlertDialog.setMessage("Select Picture Mode");

        myAlertDialog.setPositiveButton("Gallery", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface arg0, int arg1)
                    {
                        Utility.pictureActionIntent = new Intent(Intent.ACTION_GET_CONTENT, null);
                        Utility.pictureActionIntent.setType("image/*");
                        Utility.pictureActionIntent.putExtra("return-data", true);
                        startActivityForResult(Utility.pictureActionIntent, Utility.GALLERY_PICTURE);
                    }
            });

        myAlertDialog.setNegativeButton("Camera", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface arg0, int arg1)
                    {
                        Utility.pictureActionIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(Utility.pictureActionIntent, Utility.CAMERA_PICTURE);
                    }
            });
        myAlertDialog.show();

    }

    public void Image_Selecting_Task(Intent data)
    {
        ImageView user_photo = (ImageView) findViewById(R.id.user_photo);

        try
            {
                Utility.uri = data.getData();
                if (Utility.uri != null)
                    {
                        // User had pick an image.
                        Cursor cursor = getContentResolver().query(Utility.uri, new String[]
                            { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
                        cursor.moveToFirst();
                        // Link to the image
                        final String imageFilePath = cursor.getString(0);

                        //Assign string path to File
                        Utility.Default_DIR = new File(imageFilePath);

                        // Create new dir MY_IMAGES_DIR if not created and copy image into that dir and store that image path in valid_photo
                        Utility.Create_MY_IMAGES_DIR();

                        // Copy your image 
                        Utility.copyFile(Utility.Default_DIR, Utility.MY_IMG_DIR);

                        // Get new image path and decode it
                        Bitmap b = Utility.decodeFile(Utility.Paste_Target_Location);

                        // use new copied path and use anywhere 
                        String valid_photo = Utility.Paste_Target_Location.toString();
                        b = Bitmap.createScaledBitmap(b, 150, 150, true);

                        //set your selected image in image view
                        user_photo.setImageBitmap(b);
                        cursor.close();

                    } else
                    {
                        Toast toast = Toast.makeText(this, "Sorry!!! You haven't selecet any image.", Toast.LENGTH_LONG);
                        toast.show();
                    }
            } catch (Exception e)
            {
                // you get this when you will not select any single image 
                Log.e("onActivityResult", "" + e);

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

    //Calling code for different selected menu options
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()) {

            //show settings activity screen (main preference activity file)
            case R.id.wardrobe:
                Intent intent = new Intent(wardrobe.this, wardrobe.class);
                startActivity(intent);

            //if index button clicked in menu sub-menu options
            case R.id.matches:
                Toast.makeText(this, "matches was clicked!", 5).show();

            //if index button clicked in menu sub-menu options
            case R.id.worn:
                Toast.makeText(this, "worn was clicked!", 5).show();

            default:
        }

        return super.onOptionsItemSelected(item);
    }

    private class dataSend extends AsyncTask<JSONObject, Integer, Double> {

        protected Double doInBackground(JSONObject... params) {
            // TODO Auto-generated method stub
            postData(params[0]);
            return null;
        }

        protected void onPostExecute(Double result) {
            progressBarField.setVisibility(View.GONE);
            Toast.makeText(wardrobe.this, "info sent", Toast.LENGTH_LONG).show();
        }

        protected void onProgressUpdate(Integer... progress) {
            progressBarField.setProgress(progress[0]);
        }

        public void postData(JSONObject dataWardrobe) {

            Log.v("posting data", "poooooost");
            // Create a new HttpClient and Post Header
            //int TIMEOUT_MILLISEC = 10000;  // = 10 seconds
            HttpParams httpParams = new BasicHttpParams();
            //HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
            //HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
            HttpClient httpclient = new DefaultHttpClient(httpParams);

            HttpPost httppost = new HttpPost("http://127.0.0.1:3000/wardrobe");
            Log.v("posteed", "posteed url");
            try {
                Log.v("trying data", "prep");
                //add data
                 StringEntity se = new StringEntity( dataWardrobe.toString());  
                 se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                 httppost.setEntity(se);
                Log.v("posteed", "posteed 11");

                // execute http post request
                HttpResponse response = httpclient.execute(httppost);
                Log.v("posteed", "posteed 22");

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
        }

    }



}

不确定我做错了什么,我尝试了各种方法,并尝试寻找不同的方法来进行此操作,但这些方法都没有起作用……也许这比我所看到的更简单……我认为问题出在哪里在这个班级的私人班级中。

阿什利

我尚未详细阅读您的代码,但我怀疑这是一个有力的贡献者:

    HttpPost httppost = new HttpPost("http://127.0.0.1:3000/wardrobe");

如果使用的是仿真器,则很可能希望连接到“ 10.0.2.2”。这是:

主机回送接口的特殊别名(即,开发计算机上的127.0.0.1)

有关仿真器网络的更多详细信息,请参见此处:http : //developer.android.com/tools/devices/emulator.html#emulatornetworking

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何检测开机自检请求(烧瓶)上的错误?

来自分类Dev

Spring RESTtemplate开机自检

来自分类Dev

开机自检错误

来自分类Dev

Spring MVC Ajax开机自检

来自分类Dev

开机自检后隐藏DIV

来自分类Dev

Rails HTTP-开机自检

来自分类Dev

C#WebApplication开机自检

来自分类Dev

从Android版Chrome开机自检到Laravel 4控制器不起作用

来自分类Dev

AngularJS-开机自检后刷新

来自分类Dev

开机自检后未显示警报

来自分类Dev

PHP的变量和形式方法开机自检

来自分类Dev

486计算机不开机自检

来自分类Dev

复选框和开机自检

来自分类Dev

开机自检后缺少密码值

来自分类Dev

开机自检(POST)无法识别键盘

来自分类Dev

齐射发送开机自检参数始终为空

来自分类Dev

Node.js和Express; 开机自检404

来自分类Dev

jQuery开机自检按钮单击不起作用

来自分类Dev

开机自检期间如何解决此422错误?

来自分类Dev

连接硬盘会导致计算机无法开机自检

来自分类Dev

安装了新的主板,电脑无法开机自检

来自分类Dev

PHP的bind_param使用变量或开机自检?

来自分类Dev

齐射发送开机自检参数始终为空

来自分类Dev

电脑过长的时间才能开机自检,启动或关闭电源

来自分类Dev

Angular.js:开机自检后视图未更新列表

来自分类Dev

非OS HDD格式后机器不开机自检

来自分类Dev

尘土飞扬的RAM插槽无法开机自检吗?

来自分类Dev

为什么我新建的PC无法开机自检?

来自分类Dev

开机修复:发生错误