Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HttpPost在android应用程序中不工作?_Android - Fatal编程技术网

HttpPost在android应用程序中不工作?

HttpPost在android应用程序中不工作?,android,Android,我是Android应用开发的新手。我有一个web服务,它以xml格式为我提供一些数据。现在我想在onPostExecute方法中使用该数据。但当我运行它时,我的应用程序会自动停止并崩溃。 我的代码如下:- public class PalletInfo extends AsyncTask<String, Integer, String> { ProgressDialog progressDialog; public static String UPCNo="";

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

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) {

        }
    }
}
公共类PalletInfo扩展异步任务{
进行对话进行对话;
公共静态字符串UPCNo=“”;
公共静态字符串PONo=“”;
公共静态字符串LOTNo=“”;
公共静态字符串ExpDate=“”;
公共静态字符串RecDate=“”;
公共静态字符串PalateNo=“”;
公共静态字符串ItemNo=“”;
公共静态字符串ItemDesc=“”;
//公共静态字符串大小写=”;
公共静态字符串msg=“”;
字符串replyCode=“”;
字符串响应=”;
公共静态字符串scanedBarCode=“”;
活动法;
AlertDialog.Builder警报;
公共PalletInfo(活动法案、字符串扫描条形码、上下文){
progressDialog=新建progressDialog(上下文);
this.scanedBarCode=扫描条形码;
这个动作=动作;
alert=新建AlertDialog.Builder(上下文);
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
progressDialog.setMessage(“请稍候…”);
progressDialog.setCancelable(假);
progressDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
ArrayList arraystr=新的ArrayList();
字符串[]条形码=新字符串[2];
条码[0]=“条码”;
barcode[1]=scanedBarCode;//为获取响应而在服务中发送的id。
arraystr.add(条形码);
response=HttpPostRequest.doPost(“http://gcitm.com/Verify.asmx/ScanProduct,arraystr,新的DefaultHttpClient());
Log.i(“,”信息中的响应==“+响应);
InputSource is=新的InputSource();
is.setCharacterStream(新StringReader(响应));
SAXParserFactory spf=SAXParserFactory.newInstance();
saxsp=null;
试一试{
sp=spf.newSAXParser();
} 
捕获(ParserConfiguration异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
} 
捕获(SAXE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//获取我们创建的SAXParser的XMLReader。
XMLReader xr=null;
试一试{
xr=sp.getXMLReader();
} 
捕获(SAXE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
MyExampleHandler1 myExampleHandler=新的MyExampleHandler1();
xr.setContentHandler(myExampleHandler);
试一试{
xr.parse(is);
//Log.i(“,”状态代码==“+conn.getResponseCode());
} 
捕获(格式错误){
Log.d(“网络断开连接”、“网络断开连接”);
//TODO自动生成的捕捉块
e、 printStackTrace();
} 
捕获(IOE异常){
Log.d(“网络断开连接”、“网络断开连接”);
//TODO自动生成的捕捉块
e、 printStackTrace();
} 
捕获(SAXE异常){
Log.d(“网络断开连接”、“网络断开连接”);
//TODO自动生成的捕捉块
e、 printStackTrace();
}   
返回“”;
}
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
//ArrayList contactList=新建ArrayList();
progressDialog.disclose();
//如果(replyCode.equals(“1”){
alert.setMessage(replyCode);
alert.show();
//意向意向=新意向(act,PalletInformation.class);
//行动。触觉(意图);
//}
}
类MyExampleHandler1扩展了DefaultHandler{
@凌驾
public void startDocument()引发异常{
//this.myParsedExampleDataSet=新的ParsedExampleDataSet();
}
@凌驾
public void endDocument()引发异常{
//无事可做
}
@凌驾
public void startElement(字符串namespaceURI、字符串localName、字符串qName、属性atts)引发异常{
//progressDialog.disclose();
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(“receivegDate”).trim();
//Cases=atts.getValue(“Cases”).trim();
msg=atts.getValue(“msg”).trim();
}        
//Log.i(“,”产品数据==“+upcno);
/*msg=atts.getValue(“msg”);
Log.v(“replyCode-->”,“+replyCode+”“+msg+”------------Sid”+atts.getValue(“Sid”)+“------------bytes”+atts.getValue(“totalbytes”);
if(replyCode.equalsIgnoreCase(“success”)){
Form5.successsg=真;
}*/
}
}   
@凌驾
公共void endElement(字符串namespaceURI、字符串localName、字符串qName)引发SAXException{
}
@凌驾
公共无效字符(字符ch[],整数开头,整数
    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;
}