HttpPost无法在Android应用程序中工作?

时间:2014-04-08 13:00:11

标签: android

我是Android应用开发的新手。我有一个Web服务,它以xml格式提供了一些数据。现在我想在onPostExecute方法中使用该数据。但是当我运行它时,我的应用程序会自动停止并且崩溃。 我的代码是As Follows: -

public class PalletInfo extends AsyncTask<String, Integer, String> {

    ProgressDialog progressDialog;
    public static String UPCNo="";
    public static String PONo="";
    public static String LOTNo="";
    public static String ExpDate="";
    public static String RecDate="";

    public static String PalateNo="";
    public static String ItemNo="";
    public static String ItemDesc="";
    //public static String Cases="";
    public static String msg="";
    String replyCode="";
    String response="";
    public static String scanedBarCode="";
    Activity act;
    AlertDialog.Builder alert;

    public PalletInfo(Activity act,String scanbarCode, Context context) {
        progressDialog = new ProgressDialog(context);
        this.scanedBarCode=scanbarCode;
        this.act=act;  
        alert=new AlertDialog.Builder(context); 
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();   
        progressDialog.setMessage("Please Wait..");           
        progressDialog.setCancelable(false);
        progressDialog.show();  
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        ArrayList<String[]> arraystr = new ArrayList<String[]>();
        String[] barcode = new String[2];
        barcode[0] = "barcode";
        barcode[1] = scanedBarCode;                  //id which is send in service for get the response.                      
        arraystr.add(barcode);  
        response=HttpPostRequest.doPost("http://gcitm.com/Verify.asmx/ScanProduct", arraystr, new DefaultHttpClient()); 
        Log.i("", "response in palate info==="+response);
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(response));
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = null;
        try {
            sp = spf.newSAXParser();
        } 
        catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // Get the XMLReader of the SAXParser we created.
        XMLReader xr = null;
        try {
            xr = sp.getXMLReader();
        } 
        catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        MyExampleHandler1 myExampleHandler = new MyExampleHandler1();
        xr.setContentHandler(myExampleHandler); 
        try {
            xr.parse(is);       
            //Log.i("", "status code===="+conn.getResponseCode());
        } 
        catch (MalformedURLException e) {
            Log.d("Net Disconnected", "NetDisconeeted");
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (IOException e) {
            Log.d("Net Disconnected", "NetDisconeeted");
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (SAXException e) {
            Log.d("Net Disconnected", "NetDisconeeted");
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
        return "";
    }


    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        //ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
        progressDialog.dismiss();       
        //if(replyCode.equals("1")){        
            alert.setMessage(replyCode);
            alert.show();    
            //Intent intent=new Intent(act, PalletInformation.class);      
           //act.startActivity(intent);

        //}
    }

    class MyExampleHandler1 extends DefaultHandler {

        @Override
        public void startDocument() throws SAXException {
            //this.myParsedExampleDataSet = new ParsedExampleDataSet();
        }

        @Override
        public void endDocument() throws SAXException {
            // Nothing to do
        }

        @Override
        public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
            //progressDialog.dismiss(); 
            if (localName.equals("Reply")) {
                replyCode=atts.getValue("replycode").trim();         
                if(replyCode.equals("1")){
                    ItemDesc= atts.getValue("ItemDescription").trim();
                    UPCNo= atts.getValue("UPCNo").trim();
                    ItemNo= atts.getValue("ItemNo").trim();
                    PalateNo= atts.getValue("PalateNo").trim();
                    PONo= atts.getValue("PONo").trim();
                    LOTNo= atts.getValue("LOTNo").trim();
                    ExpDate= atts.getValue("ExpirationDate").trim();
                    RecDate= atts.getValue("ReceievingDate").trim();            
                    // Cases=atts.getValue("Cases").trim();
                    msg=atts.getValue("msg").trim();     
                }        
                //Log.i("", "data of product==="+upcno);
                /* msg=atts.getValue("msg");         
                Log.v("replyCode------->",""+replyCode+" "+msg+"----------Sid"+ atts.getValue("sid")+"---------------bytes"+atts.getValue("totalbytes"));
                if(replyCode.equalsIgnoreCase("success")){
                    Form5.successMsg = true;
                }*/
            }
        }   

        @Override
        public void endElement(String namespaceURI, String localName, String qName) throws SAXException {

        }

        @Override
        public void characters(char ch[], int start, int length) {

        }
    }
}

有谁能告诉我我犯了什么错误? 请帮忙... 在此先感谢...

1 个答案:

答案 0 :(得分:0)

    private InputStream call(String methodName, JSONObject json,
        List<NameValuePair> header) throws IOException 
     {
    HttpPost httpPost = new HttpPost(URL);
    httpPost.setHeader("Content-Type", "application/json");
    StringEntity entity = new StringEntity(json.toString());
     entity.setContentType("application/json");
    httpPost.setEntity(entity);
    HttpParams httpParameters = new BasicHttpParams();
    int timeoutConnection = 15000;
    HttpConnectionParams.setConnectionTimeout(httpParameters,
            timeoutConnection);
    HttpConnectionParams.setTcpNoDelay(httpParameters, true);
    int timeoutSocket = 15000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    HttpResponse httpResponse = new DefaultHttpClient(httpParameters)
            .execute(httpPost);
    is = httpResponse.getEntity().getContent();
    return is;
}