目标主机的错误,而张贴的HTTP请求 [英] Target Host Error, while post a HTTP-Request

查看:108
本文介绍了目标主机的错误,而张贴的HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试做什么


我试图创造一个Loginmask我的应用程序。要检查userslogin是正确的,我创建一个HTTP-POST(JSON)到我的服务器,从那里我得到响应,如果用户是在db表或没有。

对于HTTP-POST不到风度工作的那一刻,我永诺得到这个错误:

  14 12-26:02:22.871:W / System.err的(869):java.lang.IllegalStateException:目标主机不能为空,或参数进行设置。方案= NULL,主机= NULL,PATH = api.i-v-o.ch

这似乎在我眼里真的很奇怪,因为我定义的URL路径。


就像你看到的,问题是,我不能etablish的网络连接。请告诉我在哪里的错误是在我的code,我可以修复它​​。

这将是也真的很棒,如果你能提供给我这一个很好的教程,或codeSAMPLE,我可以对我自己改正错误。

THX对您的帮助提前。

到这里,你找到我的活动和我的DataHandler的code。

LoginActivity.class:

 包de.ivocore;进口de.ivocore.service.DataHandler;
进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;公共类LoginActivity延伸活动{    公众的DataHandler JSONLogin =新的DataHandler();
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.login);        //文本费尔德
        最终用户的EditText =(EditText上)findViewById(R.id.edt_username);
        最终的EditText PWD =(EditText上)findViewById(R.id.edt_password);        //按钮
        按钮loginbt =(按钮)findViewById(R.id.bt_login);        // OnClickListner献给巢穴按钮definieren
        loginbt.setOnClickListener(新View.OnClickListener(){            公共无效的onClick(视图v){                //字符串definieren
                。最后弦乐usertxt = user.getText()的toString();
                。最后弦乐pwdtxt = pwd.getText()的toString();                如果(usertxt = NULL&放大器;!&安培;!pwdtxt = NULL){
                JSONLogin.JSONLogin(usertxt,pwdtxt);
                }其他{
                    Toast.makeText(LoginActivity.this,BITTE用户名UND Passwort eingeben!,Toast.LENGTH_SHORT);
                }
            }
        });
    }
}


DataHandler.class

 包de.ivocore.service;进口java.io.BufferedReader中;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;进口org.apache.http.Htt presponse;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.entity.StringEntity;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.message.BasicHeader;
进口org.apache.http.params.HttpConnectionParams;
进口org.apache.http.protocol.HTTP;
进口org.json.JSONObject;进口android.os.Looper;
进口android.util.Log;公共类的DataHandler {    / ** LOGIN-了Methode
     *皮毛diese了Methode弄乱EIN最后弦乐用户名sowie EIN最后的字符串密码mitgegeben werden
     *死了Me​​thodefürt死登录-Prozedur麻省理工学院DEM api.i-v-o.ch durch。
     *Zurückgebenwird死响应welche VOM服务器kommt(登录erfolgreich奥德nicht),sowie DER Sessionkey。
     * * /
    公共无效JSONLogin(最后弦乐用户名,最后的字符串密码){
        线程t =新的Thread(){// Neuen主题definieren
            公共无效的run(){// Startet automatisch德恩德螺纹gestartet wird,führt模具的Funktion AUS
                字符串的URL =api.i-v-o.ch; // URL definieren献给JSON-帖子
                尺蠖prepare()。 //书房标准化。创建新闻报池
                HttpClient的客户端=新DefaultHttpClient(); // HttpClient的definieren,handelt书房HTTP datenverkehr
                HttpConnectionParams.setConnectionTimeout(client.getParams(),10000); //超时LIMITE
                HTT presponse响应; // Htt的presponse definieren,handelt书房http响应
                JSONObject的登录名=新的JSONObject(); // JSON对象Welches的gesendet wird                尝试{
                    HttpPost后=新HttpPost(URL); // HttpPost definieren,handelt书房HTTP POST
                    login.put(用户名的用户名); //FügetDEM JSONObject的登录用户名巢穴EIN
                    login.put(密码,密码); //FügtDEM的JSONObject登录DAS Passwort EIN                    StringEntity SE =新StringEntity(登录+ login.toString()); // StringEntity definieren UND St​​ringEntity澳大利亚DEM的JSONObject登录ERSTELLEN
                    se.setContentEncoding(新BasicHeader(HTTP.CONTENT_TYPE,应用/ JSON));
                    post.setEntity(SE); //海尔wird德邮报DAS IVO核心gesendet
                    响应= client.execute(岗位); //海尔wird死响应abgefangen
                    / **响应检查
                     *海尔wird在德回应bekommen gecheckt是世界投资报告
                     * /
                    如果(响应!= NULL){//海尔在德响应IST wirdgeprüftOB某物                        StringBuilder的SB =新的StringBuilder(); // StringBuilder的UM AUS DEM响应einen字符串祖ERSTELLEN
                        RD的BufferedReader =新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent())); //响应VOM InputStreamReader的lesen UND在einen的BufferedReadereinfügen嗯Diesen表示anschliessend祖einem字符串hinzuzuzfügenUND在艾因json对象umwandeln                        串线; //字符串definieren达林IST模具später模响应
                        而((行= rd.readLine())!= NULL){//海尔wird definiert DAS明镜串线书房inhalt VOM的BufferedReader IST,DER duchgand wird麻省理工学院的readLinedurchgeführt
                            sb.append(行+\\ n); //海尔wird德串线NACH \\ n gesplittet(新行)
                            }                        字符串结果; //字符串结果definieren
                        结果= sb.toString(); //海尔wird德inhalt VOM的StringBuilder的Zum字符串结果hinzugefühgt
                        Log.d(DataHandler的,追加字符串+结果); // Gibt AUS是IM结果DRIN IST
                    }                }赶上(例外五){
                    e.printStackTrace();
                    Log.e(DataHandler的,不能Estabilish连接);
                }
                Looper.loop(); //新闻报loopen
            }
        };
        t.start(); //线程starten
    }}


logcat的:

  14 12-26:00:29.331:W / System.err的(869):java.lang.IllegalStateException:目标主机不能为空,或参数进行设置。方案= NULL,主机= NULL,PATH = api.i-v-o.ch
12-26 14:00:29.331:W / System.err的(869):在org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
12-26 14:00:29.331:W / System.err的(869):在org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
12-26 14:00:29.331:W / System.err的(869):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-26 14:00:29.331:W / System.err的(869):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-26 14:00:29.331:W / System.err的(869):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-26 14:00:29.341:W / System.err的(869):在de.ivocore.service.DataHandler $ 1.run(DataHandler.java:51)
12-26 14:00:29.341:E / DataHandler的(869):无法连接Estabilish
12-26 14:02:22.871:W / System.err的(869):java.lang.IllegalStateException:目标主机不能为空,或参数进行设置。方案= NULL,主机= NULL,PATH = api.i-v-o.ch
12-26 15:09:44.431:W / System.err的(936):的java.net.UnknownHostException:无法解析主机api.i-v-o.ch:没有与主机名关联的地址
12-26 15:09:44.441:W / System.err的(936):在java.net.InetAddress.lookupHostByName(InetAddress.java:426)
12-26 15:09:44.441:W / System.err的(936):在java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
12-26 15:09:44.441:W / System.err的(936):在java.net.InetAddress.getAllByName(InetAddress.java:220)
12-26 15:09:44.441:W / System.err的(936):在org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
12-26 15:09:44.441:W / System.err的(936):在org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-26 15:09:44.451:W / System.err的(936):在org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-26 15:09:44.451:W / System.err的(936):在org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
12-26 15:09:44.451:W / System.err的(936):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-26 15:09:44.481:W / System.err的(936):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-26 15:09:44.501:W / System.err的(936):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-26 15:09:44.501:W / System.err的(936):在de.ivocore.service.DataHandler $ 1.run(DataHandler.java:51)
12-26 15:09:44.501:W / System.err的(936):libcore.io.GaiException:产生的原因的getaddrinfo失败:EAI_NODATA(不与主机名关联的地址)
12-26 15:09:44.511:W / System.err的(936):在libcore.io.Posix.getaddrinfo(本机方法)
12-26 15:09:44.511:W / System.err的(936):在libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
12-26 15:09:44.511:W / System.err的(936):在java.net.InetAddress.lookupHostByName(InetAddress.java:411)
12-26 15:09:44.511:W / System.err的(936):10 ...更多
12-26 15:09:44.511:E / DataHandler的(936):无法连接Estabilish


解决方案

添加的http://,以api.i-v-o.ch

因此​​,它应该是:http://api.i-v-o.ch

What I try to do


I'm trying to create a Loginmask for my App. To check if a userslogin is correct I create a HTTP-Post(JSON) to my server, from there I get response if the user is in the db-table or not.

For the moment the HTTP-POST dosn't work, I allways get this Error:

12-26 14:02:22.871: W/System.err(869): java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=api.i-v-o.ch

This seems really strange in my eyes because i defined the URL-Path.

Question


Like you saw, the problem is that i can't etablish a Network Connection. Please tell me where the error is in my code, that I can fix it.

It would be also really great, if you could provide me a good tutorial for this, or a codesample, that I can fix the error on my own.

Thx for your help in Advance.

Down here you find the Code of my Activity and my DataHandler.

LoginActivity.class:

package de.ivocore;

import de.ivocore.service.DataHandler;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {

    public DataHandler JSONLogin = new DataHandler();


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        //Text-Felder 
        final EditText user = (EditText) findViewById(R.id.edt_username);
        final EditText pwd = (EditText) findViewById(R.id.edt_password);

        //Button
        Button loginbt = (Button) findViewById(R.id.bt_login);



        //OnClickListner für den Button definieren
        loginbt.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v){

                //Strings definieren
                final String usertxt = user.getText().toString();
                final String pwdtxt = pwd.getText().toString();

                if (usertxt != null && pwdtxt != null){
                JSONLogin.JSONLogin(usertxt, pwdtxt);
                }else{
                    Toast.makeText(LoginActivity.this, "Bitte Username und Passwort eingeben!", Toast.LENGTH_SHORT);
                }
            }
        });
    }
}


DataHandler.class

package de.ivocore.service;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;

import android.os.Looper;
import android.util.Log;

public class DataHandler {

    /** LOGIN-Methode
     * Für diese Methode muss ein "final String username" sowie ein "final String password" mitgegeben werden
     * Die Methode fürt die Login-Prozedur mit dem api.i-v-o.ch durch.
     * Zurückgeben wird die Response welche vom Server kommt (Login erfolgreich oder nicht), sowie der Sessionkey.
     *  */
    public void JSONLogin(final String username, final String password){


        Thread t = new Thread(){                                                            //Neuen Thread definieren


            public void run(){                                                                  //Startet automatisch wenn der Thread gestartet wird, führt die Funktion aus


                String URL = "api.i-v-o.ch";                                                            //URL definieren für JSON-Post
                Looper.prepare();                                                                       //Erstellt den Nachrichten-Pool
                HttpClient client = new DefaultHttpClient();                                            //HttpClient definieren, handelt den http datenverkehr
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);                   //Timeout Limite
                HttpResponse response;                                                                  //HttpResponse definieren, handelt den http response
                JSONObject login = new JSONObject();                                                    //JSON-Object welches gesendet wird

                try{
                    HttpPost post = new HttpPost(URL);                                                  //HttpPost definieren, handelt den http post
                    login.put("username", username);                                                    //Füget dem JSONObject Login den Username ein
                    login.put("password", password);                                                    //Fügt dem JSONObject Login das Passwort ein

                    StringEntity se = new StringEntity("Login" + login.toString());                     //StringEntity definieren und StringEntity aus dem JSONObject Login erstellen
                    se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                    post.setEntity(se);                                                                 //Hier wird der Post an das IVO-Core gesendet
                    response = client.execute(post);                                                    //Hier wird die Response abgefangen


                    /** Response-Checker
                     * Hier wird gecheckt was wir in der Response bekommen
                     */
                    if(response != null){                                                               //Hier wird geprüft ob etwas in der Response ist

                        StringBuilder sb = new StringBuilder();                                         //Stringbuilder um aus dem Response einen String zu erstellen
                        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));   //response vom inputstreamreader lesen und in einen bufferedreader einfügen um diesen anschliessend zu einem string hinzuzuzfügen und in ein json object umwandeln

                        String line;                                                                    //String definieren darin ist die später die response
                        while ((line = rd.readLine()) != null) {                                        //Hier wird definiert das der String Line den inhalt vom bufferedreader ist, der duchgand wird mit readLine durchgeführt
                            sb.append(line + "\n");                                                     //Hier wird der string line nach \n gesplittet (newline)
                            }

                        String result;                                                                  //String result definieren
                        result = sb.toString();                                                         //Hier wird der inhalt vom Stringbuilder zum String result hinzugefühgt
                        Log.d("DataHandler", "Append String " + result);                                //Gibt aus was im result drin ist


                    }

                } catch(Exception e){
                    e.printStackTrace();
                    Log.e("DataHandler", "Cannot Estabilish Connection");
                }
                Looper.loop();                                                                          //Nachrichten loopen
            }
        };
        t.start();                                                                                      //Thread starten
    }

}


Logcat:

12-26 14:00:29.331: W/System.err(869): java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=api.i-v-o.ch
12-26 14:00:29.331: W/System.err(869):  at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
12-26 14:00:29.331: W/System.err(869):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
12-26 14:00:29.331: W/System.err(869):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-26 14:00:29.331: W/System.err(869):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-26 14:00:29.331: W/System.err(869):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-26 14:00:29.341: W/System.err(869):  at de.ivocore.service.DataHandler$1.run(DataHandler.java:51)
12-26 14:00:29.341: E/DataHandler(869): Cannot Estabilish Connection
12-26 14:02:22.871: W/System.err(869): java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=api.i-v-o.ch
12-26 15:09:44.431: W/System.err(936): java.net.UnknownHostException: Unable to resolve host "api.i-v-o.ch": No address associated with hostname
12-26 15:09:44.441: W/System.err(936):  at java.net.InetAddress.lookupHostByName(InetAddress.java:426)
12-26 15:09:44.441: W/System.err(936):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
12-26 15:09:44.441: W/System.err(936):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
12-26 15:09:44.441: W/System.err(936):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
12-26 15:09:44.441: W/System.err(936):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-26 15:09:44.451: W/System.err(936):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-26 15:09:44.451: W/System.err(936):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
12-26 15:09:44.451: W/System.err(936):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-26 15:09:44.481: W/System.err(936):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-26 15:09:44.501: W/System.err(936):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-26 15:09:44.501: W/System.err(936):  at de.ivocore.service.DataHandler$1.run(DataHandler.java:51)
12-26 15:09:44.501: W/System.err(936): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
12-26 15:09:44.511: W/System.err(936):  at libcore.io.Posix.getaddrinfo(Native Method)
12-26 15:09:44.511: W/System.err(936):  at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
12-26 15:09:44.511: W/System.err(936):  at java.net.InetAddress.lookupHostByName(InetAddress.java:411)
12-26 15:09:44.511: W/System.err(936):  ... 10 more
12-26 15:09:44.511: E/DataHandler(936): Cannot Estabilish Connection

解决方案

Add "http://" to "api.i-v-o.ch"

So it should be: "http://api.i-v-o.ch"

这篇关于目标主机的错误,而张贴的HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆