Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.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
Java InputStream is=conn.getInputStream()不';无法获取数据流-->;应用程序崩溃_Java_Android_Webrequest - Fatal编程技术网

Java InputStream is=conn.getInputStream()不';无法获取数据流-->;应用程序崩溃

Java InputStream is=conn.getInputStream()不';无法获取数据流-->;应用程序崩溃,java,android,webrequest,Java,Android,Webrequest,这是一个来自Digi设备云的代码,稍加修改。我没有任何输入流,因此我的应用程序崩溃。我正在尝试在我的Android应用程序上显示来自Digi设备的数据。试着调试它,应用程序在运行到 InputStream is=conn.getInputStream() 我进入调试器: com.android.okhttp.internal.http.HttpsURLConnectionImpl$httpurlconnectionelegate:https://devicecloud.digi.com/ws/D

这是一个来自Digi设备云的代码,稍加修改。我没有任何
输入流
,因此我的应用程序崩溃。我正在尝试在我的Android应用程序上显示来自Digi设备的数据。试着调试它,应用程序在运行到

InputStream is=conn.getInputStream()

我进入调试器:

com.android.okhttp.internal.http.HttpsURLConnectionImpl$httpurlconnectionelegate:https://devicecloud.digi.com/ws/DataStream/00000000-00000000-00409DFF-FF60FE08/xbee.serialIn
。在这一行之后,它直接跳到catch(异常e)

查看
getInputStream()
,它似乎没有得到覆盖。因为这个问题,我已经在这里搜索过了,但是找不到解决方案,也许我只是忽略了一些东西

public InputStream getInputStream() throws IOException {
    throw new UnknownServiceException("Does not support writing to the input stream");
}
代码如下:

public class InputXML extends ActionBarActivity {

    TextView view_xml;
    private String encodedAuthorization,responseContent;


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

        XMLreference();
        String request = getRequest().toString();
        view_xml.setText(request);

    }

        /**
         * Run the web service request
         */
        public String getRequest() {
            HttpsURLConnection conn = null;

            final String username = "username"; // put your Device Cloud username here
            final String password = "password"; // put your Device Cloud password here

            try {
                // Create url to the Device Cloud server for a given web service request
                URL url = new URL("https://devicecloud.digi.com/ws/DataStream/00000000-00000000-00409DFF-FF60FE08/xbee.serialIn");
                conn = (HttpsURLConnection) url.openConnection();
                conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.setRequestMethod("GET");

                // Build authentication string
                String userpassword = username + ":" + password;

                // can change this to use a different base64 encoder
                // String encodedAuthorization =  Base64.encodeBase64String(userpassword.getBytes()).trim();
                byte[] data = null;
                try {
                    data = userpassword.getBytes("UTF-8");
                } catch (UnsupportedEncodingException e1) {
                    e1.printStackTrace();
                }

                encodedAuthorization = Base64.encodeToString(data, Base64.DEFAULT).trim();


                // set request headers
                conn.setRequestProperty("Authorization", "Basic "
                        + encodedAuthorization);
                // Get input stream from response and convert to String
                InputStream is = conn.getInputStream();

                Scanner isScanner = new Scanner(is);
                StringBuffer buf = new StringBuffer();
                while (isScanner.hasNextLine()) {
                    buf.append(isScanner.nextLine() + "\n");
                    Toast.makeText(getBaseContext(),"in while schleife",Toast.LENGTH_LONG);
                }
                responseContent = buf.toString();

                // add line returns between tags to make it a bit more readable
                responseContent = responseContent.replaceAll("><", ">\n<");
                //responseContent = "Es übergibt weiter";

                // Output response to standard out
                System.out.println(responseContent);
            } catch (Exception e) {
                // Print any exceptions that occur
                e.printStackTrace();
            } finally {
                if (conn != null)
                    conn.disconnect();
            }
            return responseContent;
        }



    @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_input_xml, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void XMLreference(){
        view_xml = (TextView) findViewById(R.id.view_xml);
    }
}
公共类InputXML扩展了ActionBarActivity{
TextView视图_xml;
私有字符串编码授权、响应内容;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u input\u xml);
XMLreference();
字符串请求=getRequest().toString();
view_xml.setText(请求);
}
/**
*运行web服务请求
*/
公共字符串getRequest(){
HttpsURLConnection conn=null;
最终字符串username=“username”//将您的设备云用户名放在此处
final String password=“password”//在此处输入您的设备云密码
试一试{
//为给定的web服务请求创建设备云服务器的url
URL=新URL(“https://devicecloud.digi.com/ws/DataStream/00000000-00000000-00409DFF-FF60FE08/xbee.serialIn");
conn=(HttpsURLConnection)url.openConnection();
连接设置输出(真);
conn.setDoInput(真);
conn.setRequestMethod(“GET”);
//生成身份验证字符串
字符串userpassword=用户名+“:”+密码;
//可以将此更改为使用不同的base64编码器
//String encodedAuthorization=Base64.encodeBase64String(userpassword.getBytes()).trim();
字节[]数据=null;
试一试{
data=userpassword.getBytes(“UTF-8”);
}捕获(不支持DencodingException e1){
e1.printStackTrace();
}
encodedAuthorization=Base64.encodeToString(数据,Base64.DEFAULT).trim();
//设置请求头
conn.setRequestProperty(“授权”、“基本”
+编码(授权);
//从响应中获取输入流并转换为字符串
InputStream is=conn.getInputStream();
扫描仪isScanner=新扫描仪(is);
StringBuffer buf=新的StringBuffer();
while(isScanner.hasNextLine()){
buf.append(isScanner.nextLine()+“\n”);
Toast.makeText(getBaseContext(),“in while schleife”,Toast.LENGTH_LONG);
}
responseContent=buf.toString();
//在标记之间添加行返回,使其更具可读性

responseContent=responseContent.replaceAll(">\n\n您不能在主线程中执行Http操作!您应该在单独的线程上执行此操作,并将结果传递给主线程。

您的意思是创建一个新类,然后在主线程中调用我的构造函数来调用结果吗?我已经尝试过了,但也不起作用。不,请看这个:谢谢,我现在将阅读它。我希望我的英语和程序正确明的技能足以理解它。
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import javax.net.ssl.HttpsURLConnection;
import java.net.URL;
import java.util.Scanner;

/* Can replace this with any base 64 encoder for basic authentication. For java 6 
 * installations on Sun's JRE you can use "sun.misc.BASE64Encoder" however this will 
 * not work in some installations (using OpenJDK).  Java mail 
 * (javax.mail.internet.MimeUtility) also contains a Base 64 encoder in Java 6.  A 
 * public domain version exists at http://iharder.sourceforge.net/current/java/base64/
 */
import org.apache.commons.codec.binary.Base64;

/**
 * This is a stub class with a main method to run a Device Cloud web service.
 */
public class WebServiceRequest {
    private static final String username = "username"; // put your Device Cloud username here
    private static final String password = "password"; // put your Device Cloud password here

    /**
     * Run the web service request
     */
    public static void main(String[] args) {
        HttpsURLConnection conn = null;

        try {
            // Create url to the Device Cloud server for a given web service request
            URL url = new URL("https://devicecloud.digi.com/ws/DataStream/00000000-00000000-00409DFF-FF60FE08/xbee.serialIn");
            conn = (HttpsURLConnection) url.openConnection(); 
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("GET"); 

            // Build authentication string
            String userpassword = username + ":" + password;

            // can change this to use a different base64 encoder
            String encodedAuthorization = Base64.encodeBase64String(userpassword.getBytes()).trim();

            // set request headers
            conn.setRequestProperty("Authorization", "Basic "
                    + encodedAuthorization);
// Get input stream from response and convert to String
            InputStream is = conn.getInputStream();

            Scanner isScanner = new Scanner(is);
            StringBuffer buf = new StringBuffer();
            while (isScanner.hasNextLine()) {
                buf.append(isScanner.nextLine() + "\n");
            }
            String responseContent = buf.toString();

            // add line returns between tags to make it a bit more readable
            responseContent = responseContent.replaceAll("><", ">\n<");

            // Output response to standard out
            System.out.println(responseContent);
        } catch (Exception e) {
            // Print any exceptions that occur
            e.printStackTrace();
        } finally {
            if (conn != null)
                conn.disconnect();
        }
    }
}