0
java.net.MalformedURLException: no protocol: "localhost/uatpw/ActiveTransaction"?isx=E13F42EC5E38
    at java.net.URL.<init>(URL.java:567)
    at java.net.URL.<init>(URL.java:464)
    at java.net.URL.<init>(URL.java:413)

从包含 localhost 的 url 读取数据时出现格式错误的 URL 异常。

其实我的程序如下

package bll.sap;

import java.net.MalformedURLException;

import utility.PropertyUtility;

public class GetActiveData 
{
    public static void main(String[] args) {

        String sapURL = "";

        try {


            sapURL = PropertyUtility.getSapURL();


            //Get Summary Data
            SapDataSync sapDataSync = new SapDataSync();

            //sapDataSync.readTransactionJsonFromUrl(sapURL+"?isx=false");
            sapDataSync.readTransactionJsonFromUrl(sapURL+"?isx=E13F42EC5E38");

            } 
        catch(MalformedURLException me)
        {
            me.printStackTrace();
        }   
        catch (Exception e) {
                e.printStackTrace();
        }
    }









}

package bll.sap;




    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.nio.charset.Charset;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;

    import net.sf.json.JSONArray;
    import net.sf.json.JSONException;
    import net.sf.json.JSONObject;
    import utility.Utility;

    import com.google.code.morphia.Datastore;

    import dal.GetMorphiaDB;

    public class SapDataSync 
    {

        private void saveSapTransaction(List<TransactionUnit> sapTransaction){
            GetMorphiaDB morphia;
            try {
                morphia = GetMorphiaDB.getInstance();
                Datastore ds = morphia.getDs();
                ds.save(sapTransaction);
            }catch (Exception e) {
                e.printStackTrace();
            }
        }

        private void createSapTransaction(String transactionJson){
            JSONObject jsonObj = JSONObject.fromObject(transactionJson);

            JSONArray transactionUnits = jsonObj.getJSONArray("TRANSACTION");


            List<ActiveUnit> transactionList = new ArrayList<ActiveUnit>();
            for(int i = 0; i < transactionUnits.size() ; i++){
                JSONObject jsn = transactionUnits.getJSONObject(i);
                JSONObject jsnFeed = transactionUnits.getJSONObject(i);

                ActiveUnit transactionUnit = new ActiveUnit(
                        jsn.getString("listEditions"),
                        jsn.getString("listPackage"),
                        //Double.parseDouble(jsn.getString("YIELD")),
                        //Double.parseDouble(jsn.getString("QUANTITY")),
                        //Double.parseDouble(jsn.getString("VALUE")),
                        jsn.getString("referenceID")
                        //jsn.getString("PRICEGROUP"),
                        //jsn.getString("PAGE"),
                        //Utility.getCalendarTime(jsn.getString("PUBDATE"), "dd-MM-yyyy"),
                        //jsn.getString("CLIENTNAME"),
                        //jsn.getString("CLIENTCODE"),
                        // new Date().getTime(),
                         //jsn.getString("BOOKINGUNITNAME"),
                            //jsn.getString("BCCNAME"),
                            //jsn.getString("PAGENAME"),
                        //  jsn.getString("PRICEGROUPNAME"),
                        //  jsn.getString("ORDER"),
                        //  jsn.getString("PAGE_LH_RH")
                        );
                transactionList.add(transactionUnit);
                System.out.println(transactionList);    
            }

            System.out.println(transactionList.size());
            if (transactionList.size() > 0) {
                //saveSapTransaction(transactionList);
            }

        }

        public void readTransactionJsonFromUrl(String url) throws IOException, JSONException {
            InputStream is = new URL(url).openStream();
            try {
              BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
              StringBuilder sb = new StringBuilder();
              int cp;
              while ((cp = rd.read()) != -1) {
                sb.append((char) cp);
              }
              createSapTransaction(sb.toString());
            } finally {
              is.close();
            }
          }

    }

package bll.sap;

import java.io.Serializable;
import java.util.Date;

import org.bson.types.ObjectId;

import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;

@Entity("SapTransaction")
public class ActiveUnit implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private ObjectId id;
    private Long  tilCreationDate;
    private String clientName;
    private String clientCode;
    private String listEditions;
    private Long date;
    private String listPackage;
    private String bookingUnitName;
    private String referenceID;
    private String bccName;
    private String page;
    private String pageName;
    private String priceGroup;
    private String pgName;
    private Double yield;
    private Double qty;
    private Double value;
    private String order;
    private String pageType;

    public ActiveUnit() {
    }
    public ActiveUnit(String listEdtions, String listPackage, /*Double yield,
            Double qty, Double value,*/ String referenceID  /*, String priceGroup,
            String page, Long date,String clientName,
            String clientCode,Long  tilCreationDate,String bookingUnitName,String bccName,String pageName,String pgName,String order,String pageType*/) {
        this.listEditions = listEdtions;
        this.listPackage = listPackage;
        //this.yield = yield;
        //this.qty = qty;
        //this.value = value;
        this.referenceID = referenceID;
        //this.priceGroup = priceGroup;
        //this.page = page;
        //this.date = date;
        //this.clientName = clientName;
        //this.clientCode = clientCode;
        //this.tilCreationDate = tilCreationDate;
        //this.setBookingUnitName(bookingUnitName);
        //this.bccName = bccName;
        //this.pageName = pageName;
        //this.pgName = pgName;
        //this.order = order;
        //this.pageType = pageType;
    }

    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
    }

    public String getClientCode() {
        return clientCode;
    }

    public void setClientCode(String clientCode) {
        this.clientCode = clientCode;
    }

    public void setId(ObjectId id) {
        this.id = id;
    }

    public ObjectId getId() {
        return id;
    }

    public void setTilCreationDate(Long tilCreationDate) {
        this.tilCreationDate = tilCreationDate;
    }

    public Long getTilCreationDate() {
        return tilCreationDate;
    }
    public String getreferenceID() {
        return referenceID;
    }

    public void setreferenceID(String referenceID) {
        this.referenceID = referenceID;
    }

    public String getPriceGroup() {
        return priceGroup;
    }

    public void setPriceGroup(String priceGroup) {
        this.priceGroup = priceGroup;
    }

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public Long getDate() {
        return date;
    }

    public void setDate(Long date) {
        this.date = date;
    }


    public String getListEditions() {
        return listEditions;
    }

    public void setVertical(String listEditions) {
        this.listEditions = listEditions;
    }

    public String getListPackage() {
        return listPackage;
    }

    public void setListPackage(String listPackage) {
        this.listPackage = listPackage;
    }

    public Double getYield() {
        return yield;
    }

    public void setYield(Double yield) {
        this.yield = yield;
    }

    public Double getQty() {
        return qty;
    }

    public void setQty(Double qty) {
        this.qty = qty;
    }

    public Double getValue() {
        return value;
    }

    public void setValue(Double value) {
        this.value = value;
    }

    public void setBookingUnitName(String bookingUnitName) {
        this.bookingUnitName = bookingUnitName;
    }

    public String getBookingUnitName() {
        return bookingUnitName;
    }

    public String getBccName() {
        return bccName;
    }

    public void setBccName(String bccName) {
        this.bccName = bccName;
    }

    public String getPageName() {
        return pageName;
    }

    public void setPageName(String pageName) {
        this.pageName = pageName;
    }

    public String getPgName() {
        return pgName;
    }

    public void setPgName(String pgName) {
        this.pgName = pgName;
    }

    @Override
    public String toString() {
        String unit = "{ " +
                            //"ClientCode: "  + this.clientCode+
                            //",TILCreation Date: "  + new Date(this.tilCreationDate)+
                            //",ClientName: "+ this.clientName+
                            "listEditions: "  + this.listEditions+
                            ",listPackage: "+ this.listPackage+ 
                            //",BookingUnitName: "+ this.bookingUnitName+ 
                            //",Yield: "     + this.yield+
                            //",QTY: "  + this.qty+
                            //",Value: "  + this.value+
                            ",referenceID: "  + this.referenceID+
                            //",Price Group: "  + this.priceGroup+
                            //",BCCName: "  + this.bccName+
                            //",PageName: "  + this.pageName+
                            //",PriceGroupName: "  + this.pgName+
                            //",Page: "  + this.page+
                            //",PageType: "  + this.pageType+
                            //",Order: "  + this.order+
                            //",PublishDate: "  + new Date(this.date) +

                    " }";
        return unit;
    }
    public void setOrder(String order) {
        this.order = order;
    }
    public String getOrder() {
        return order;
    }
    public void setPageType(String pageType) {
        this.pageType = pageType;
    }
    public String getPageType() {
        return pageType;
    }
}
4

3 回答 3

8

首先,确保您已为您的请求设置了协议。

其次,确保String包含 URL 的 URL 是 URL 编码的。即 URL 没有任何空格和其他特殊字符 - 这些应该被编码(空格是 %20 等)。

鉴于满足上述两个条件,您的程序不应从java.net.URL类中抛出异常。

查看上面的异常,您只需设置协议 ( http://),但请确保正确编码 URL 地址字符串,否则您会从程序的其他部分获得异常。

此外,添加http://到以下字符串也将导致MalformedURLException: "localhost/uatpw/ActiveTransaction"?isx=E13F42EC5E38因为您的 URL 将包含"需要编码的特殊字符(在这种情况下)。

要提供有效的 URL,您应该确保从 URL 的服务器和路径段区域中去除引号: localhost/uatpw/ActiveTransaction?isx=E13F42EC5E38. 对此http://进行准备将产生一个有效的 URL。

于 2013-06-17T11:29:42.050 回答
3

您缺少http://localhost 前面的协议(例如 )。

格式良好的 URL 可以是http://localhost/uatpw/ActiveTransaction

于 2013-06-17T11:12:56.390 回答
0

这不是一个问题。你想做什么?

但除此之外,“localhost/uatpw/ActiveTransaction”不是有效的 URL。url 必须以协议(http、https、ftp 等)开头。

你应该尝试:

http://localhost/uatpw/ActiveTransaction
于 2013-06-17T11:13:34.230 回答